VBA error

Nurzhan

Board Regular
Joined
Dec 13, 2017
Messages
60
Hi All. Pls help me. What's wrong in the following code? The line in red in returns error. But why? Both files i keep open during running macro.
------------------------------------
Sub GL_DPR_FillIn()


' Fills in Report with up-to-date data.


Dim wbA As Workbook
Dim wbB As Workbook
Dim wsName, lastRow
Dim j


Set wbA = Workbooks("GL Rates Calculation.xlsm") 'This one should already be open
Set wbB = Workbooks("DPR_ALS.xlsx")


For j = 5 To 18 Step 1

wbA.Sheets("GL").Range("AG" & "j" & ":" & "AN" & "j").Copy






If j = 5 Then wsName = "Ch22"
If j = 6 Then wsName = "Ch24"
If j = 7 Then wsName = "Ch30"
If j = 8 Then wsName = "Ch54"
If j = 9 Then wsName = "Ch56"
If j = 10 Then wsName = "Ch60"
If j = 11 Then wsName = "Ch62"
If j = 12 Then wsName = "Ch65"
If j = 13 Then wsName = "Ch67"
If j = 14 Then wsName = "Ch115b"
If j = 15 Then wsName = "Ch117"
If j = 16 Then wsName = "Ch123"
If j = 17 Then wsName = "Ch51"
If j = 18 Then wsName = "Ch124"




lastRow = Sheets(wsName).Cells(Rows.Count, "C").End(xlUp).Row
Sheets(wsName).Range("C" & lastRow + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False


Application.CutCopyMode = False


Next j


End Sub
 
Re: VBA error help

Can you post the whole code as it is now, there's likely something missing.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Re: VBA error help

here it's:

Code:
[/COLOR]Sub DPR_HC_FillIn()' Fills in Report with up-to-date data.




 Dim wbA As Workbook, wbB As Workbook
    Dim wsName As String, j As Long


    Application.ScreenUpdating = False


Set wbA = Workbooks("Daily Production Report - December 2017.xlsm") 'This one should already be open
Set wbB = Workbooks("Single Well Daily Profile - December 2017.xlsx")  'This one should already be open


For j = 9 To 105
        wsName = ""
         
        If j = 9 Then wsName = "10"
        If j = 10 Then wsName = "22"
        If j = 11 Then wsName = "24"
        If j = 12 Then wsName = "27"
        If j = 13 Then wsName = "30"
        If j = 14 Then wsName = "33"
        If j = 15 Then wsName = "52"
        If j = 16 Then wsName = "54"
        If j = 17 Then wsName = "56"
        If j = 18 Then wsName = "59"
        If j = 19 Then wsName = "60"
        If j = 20 Then wsName = "62"
        If j = 21 Then wsName = "65"
        If j = 22 Then wsName = "67"
        If j = 23 Then wsName = "70"
        If j = 24 Then wsName = "111"
        If j = 25 Then wsName = "115b"
        If j = 26 Then wsName = "117"
        If j = 27 Then wsName = "124"
        If j = 28 Then wsName = "208"
        
        If j = 36 Then wsName = "31"
        If j = 37 Then wsName = "40"
        If j = 38 Then wsName = "45"
        If j = 39 Then wsName = "51"
        If j = 40 Then wsName = "123"
        If j = 41 Then wsName = "701"
        If j = 42 Then wsName = "703"
        If j = 43 Then wsName = "724"
        If j = 44 Then wsName = "725"
        If j = 45 Then wsName = "410"
        
        If j = 53 Then wsName = "20"
        If j = 54 Then wsName = "119"
        If j = 55 Then wsName = "204"
        If j = 56 Then wsName = "205"
        If j = 57 Then wsName = "209"
        If j = 58 Then wsName = "210"
        If j = 59 Then wsName = "215"
        If j = 60 Then wsName = "216"
        If j = 61 Then wsName = "217"
        If j = 62 Then wsName = "218"
        If j = 63 Then wsName = "219"
        If j = 64 Then wsName = "220"
        If j = 65 Then wsName = "222"
        If j = 66 Then wsName = "223"
        If j = 67 Then wsName = "225"
        If j = 68 Then wsName = "230"
        If j = 69 Then wsName = "234"
       
        If j = 77 Then wsName = "28"
        If j = 78 Then wsName = "32"
        If j = 79 Then wsName = "46"
        If j = 80 Then wsName = "115"
        If j = 81 Then wsName = "213"
        If j = 82 Then wsName = "301"
        If j = 83 Then wsName = "61"
        
        If j = 91 Then wsName = "57"
        If j = 92 Then wsName = "300"
        If j = 93 Then wsName = "303"
        
        If j = 101 Then wsName = "23"
        If j = 102 Then wsName = "401"
        If j = 103 Then wsName = "402"
        If j = 104 Then wsName = "404"
        If j = 105 Then wsName = "406"


If Not wsName = "" Then


ActiveWorkbook.ActiveSheet.Range("C" & j & ":" & "AF" & j).Copy _
                wbB.Sheets(wsName).Cells(Rows.Count, "D").End(xlUp).Offset(1, -1)




Next j


 Application.ScreenUpdating = True




End Sub


[COLOR=#574123]
 
Upvote 0
Re: VBA error help

Try
Code:
   If Not wsName = "" Then
      ActiveWorkbook.ActiveSheet.Range("C" & j & ":" & "AF" & j).Copy _
         wbB.Sheets(wsName).Cells(Rows.Count, "D").End(xlUp).Offset(1, -1)
   [COLOR=#ff0000]End If[/COLOR]
Next j
 
Upvote 0
Re: VBA error help

Great!!! It's now working! Thank you so much guys! I was trying to help my colleauge to make easy his life and same time I'm practicing VBA. I appreciate that!
 
Last edited:
Upvote 0
Re: VBA error help

Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top