Home > Mobile >  VBA How to IF NOT and go to next page
VBA How to IF NOT and go to next page

Time:02-15

I have a question, I would like to write a loop that, if one Sheet would not go to fill another, and if it does fill it, I tried so, but it pops up that it must be done by the For statement. I don't know how. I mean If this tab C_0700_0002 is missing, go to the other one for example C_0700_0007.

If wb.Sheets("C_0700_0002") Is Nothing Then
Next
Else
'C_0700_002'
wbMe.Sheets("Tabela_COREP").Range("F4").Copy
wb.Sheets("C_0700_0002").Range("G17").PasteSpecial Paste:=xlPasteValues
wbMe.Sheets("Tabela_COREP").Range("J5").Copy
wb.Sheets("C_0700_0007").Range("G17").PasteSpecial Paste:=xlPasteValues
wbMe.Sheets("Tabela_COREP").Range("J5").Copy
wb.Sheets("C_0700_0007").Range("G17").PasteSpecial Paste:=xlPasteValues

CodePudding user response:

Copy Conditionally

Dim ws As Worksheet

On Error Resume Next
    Set ws = wb.Worksheets("C_0700_0002")
On Error GoTo 0

If ws Is Nothing Then
    Set ws = wb.Worksheets("C_0700_0007")
End If

ws.Range("G17").Value = wbMe.Worksheets("Tabela_COREP").Range("J5").Value

EDIT

Const sAddressesList As String = "A1,B3,C2,D5,B6,C7,G2,F11,A17,B13"
Const dAddressesList As String = "B3,F8,G6,G15,B17,F7,A3,A7,F15,C4"
Const dNumsList As String = "1,2,3,4,5,6,7,8,9,10"

Dim wb As Workbook
Dim wbMe As Workbook

' Other code...

Dim sws As Worksheet: Set sws = wbMe.Worksheets("Tabela_COREP")

Dim dws As Worksheet
Dim n As Long

Dim sAddresses() As String: sAddresses = Split(sAddressesList, ",")
Dim dAddresses() As String: dAddresses = Split(dAddressesList, ",")
Dim dNums() As String: dNums = Split(dNumsList, ",")

For n = 0 To UBound(dNums)
    On Error Resume Next
        Set dws = wb.Worksheets("C_0700_" & Format(dNums(n), "000#"))
    On Error GoTo 0
    If Not dws Is Nothing Then
        dws.Range(dAddresses(n)).Value = sws.Range(sAddresses(n)).Value
        Set dws = Nothing
    End If
Next n

  • Related