Would like a correction in VBA coding

Meko1

New Member
Joined
Oct 20, 2020
Messages
44
Office Version
  1. 2016
Platform
  1. Windows
Hello
Since, I am new to coding, would like some of your help.

I am trying to move data from 2 workbooks into 1. I am showing you the example of what I want to do, and I am going to repeat the same for the other cells;

VBA Code:
Sub new_excel_problem()

   Workbooks ("Current"), Sheet1.Range("E3") = Workbooks("GASB worksheet updated") ActiveSheet.Range("B7")
   Workbooks ("Domain"), Sheet1.Range("D2") = Workbooks("GASB worksheet updated") ActiveSheet.Range("B11")
   
End Sub

But VBA tells me its an syntax error on the Sub, and the whole code is in red. I am guessing with using several workbooks something else needs to be done.
Please help me out.

Thank you in advance.
 
Last edited by a moderator:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You have to use periods (dots) to connect the objects like Workbook.Sheet.Range
this should get rid of the red, but unless you have 'GASB worksheet updated' open to the correct sheet at runtime, you will likely get an error. it is better to use the sheet name.

VBA Code:
Sub new_excel_problem()

Workbooks ("Current"). Sheet1.Range("E3") = ActiveSheet.Range("B7")
Workbooks ("Domain"). Sheet1.Range("D2") = ActiveSheet.Range("B11")

End Sub
 
Upvote 0
Solution
Because your VBA has syntax errors. Revise the code as shown below and test it. Change the "SheetName" with the actual sheet name in the target workbook.

VBA Code:
Sub new_excel_problem()
    ' Using With...End With block to not repeat the target worksheet object in the code.
    With Workbooks("GASB worksheet updated").Sheets("SheetName")
        .Range("B7") = Workbooks("Current").Worksheets("Sheet1").Range("E3")
        .Range("B11") = Workbooks("Domain").Worksheets("Sheet1").Range("D2")
        ' More cells to update here...
    End With
End Sub
 
Upvote 0
Follow up: When you use ActiveSheet, you do not use the parent workbook reference because that is included in the ActiveSheet constant. in other words, ActiveSheet = ParentWorkbook.Selected sheet.
 
Upvote 0
Thank you both, I am using ActiveSheet instead of name because I need to add other sheets as well and make similar action, so the sheet name will change anyways.
After I corrected dots, and opened all 3 workbooks code, it still gives me a debug and does not let me do the following. It says the error on both.
Any thoughts what can be a problem?
 
Upvote 0
You have to use periods (dots) to connect the objects like Workbook.Sheet.Range
this should get rid of the red, but unless you have 'GASB worksheet updated' open to the correct sheet at runtime, you will likely get an error. it is better to use the sheet name.

VBA Code:
Sub new_excel_problem()

Workbooks ("Current"). Sheet1.Range("E3") = ActiveSheet.Range("B7")
Workbooks ("Domain"). Sheet1.Range("D2") = ActiveSheet.Range("B11")

End Sub
OK I will try to make it work, it is a bit late here so I will try more tomorrow. If still will not work I will ask for help.
Thanks again.
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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