I am creating an excel workbook with multiple different sheets. I want to be able to add a row to any sheet and have it reflected on the first sheet. I am having trouble with this since I want to add them to different sheets based on a condition. Is there any easy way to do this.
Here is a template for having multiple worksheets in VBA and setting values b/w them
Sub test ()
Dim ws as worksheet
Dim ws2 as worksheet
Set ws = Worksheets(1)
Set ws2 = Worksheets(2)
ws2.Range("A") = ws.Range("A")
End Sub
Related
(Sorry in advance for my poor English: not first Language :) )
I am writing a VBA Excel 2003 routine that runs through 50+ worksheets in a file, copies the worksheets (just as a temp copy) one by one and then performs actions on them before removing these temporary copies and returning the result of it's calculations on the worksheets content.
To be more precise: the code is called from an external file with a single (hidden) worksheet inside. When I open the file it runs a code to create a new toolbar in Excel, when I press a button on the toolbar, the code I have described above runs.
I know that not saving a file and performing many and many copies triggers this error, but now it is triggering at the first attempt (I have closed and reopened everything multiple times to be sure I am not keeping the not saved situation with me).
This is the code triggering the problem, I am sorry for poor formatting:
ActiveWorkbook.Worksheets("NAME OF THE FIRST WORKSHEET I WANT TO COPY").Copy ThisWorkbook.Worksheets("HiddenSheet")
Disclamer: the name of the worksheet is found by a For..Next cycle through the ActiveWorkbook.Worksheets array, but the code is not working even if i hard-code the name myself.
Here is a larger chunk of the code, to be clearer:
Set sourceWorkbook = ActiveWorkbook
For index = 1 To sourceWorkbook.Worksheets.Count
sourceWorkbook.Activate 'not sure if this is even needed
Set currWorksheet = sourceWorkbook.Worksheets(index)
currWorksheet.Copy ThisWorkbook.Worksheets("HiddenSheet")
Next index
The result is now consistently:
Run-time Error '1004'
Copy method of worksheet class failed.
I thank everybody in advance for the help!
Some useful guidelines:
Option Explicit
'Copy sheet
Sub CopySheet()
Dim ws1 As Workbook, ws2 As Workbook
'It's better to declare sheets and avoid activate
Set ws1 = Workbooks("Book1")
Set ws2 = Workbooks("Book2")
'Copy sheet "Test" from ws1(Book1) to ws2 (Book2) after all sheets
ws1.Worksheets("Test").Copy After:=ws2.Worksheets(Sheets.Count)
End Sub
Option Explicit
'Copy a range
Sub CopyRange()
Dim ws1 As Workbook, ws2 As Workbook
'It's better to declare sheets and avoid activate
Set ws1 = Workbooks("Book1")
Set ws2 = Workbooks("Book2")
'Copy from ws1(Book1), sheet "Test" & range A1:A5 to ws2 (Book2), sheet "sheet1" & range A1
ws1.Worksheets("Test").Range("A1:A5").Copy
ws2.Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteValues
End Sub
I am trying to simplify some code, and wondered whether I would be able to use an If statement to set a different open workbook variable, depending on a few conditions of a defined range.
basically something like
dim ws as worksheet: set ws = Thisworkbook.worksheet("worksheet name")
dim r as range: set range = ws.range("A cell which has an if statement in it to display a number based on another cells contents")
dim wb as workbook
this is where I want to vary the actual workbook set as wb
if r.value = 1 then
set wb = Open a workbook
elseif r.value = 2 then
set wb = Open another workbook
Ideally I would then do the same for ws2 which would be the specific worksheet variable for the specified workbook above
Not sure if this method will work and wondered if I was on the right track. I have code which opens a workbook, finds values in various rows, inserts rows, copies cell contents., between two ws variables, etc. And it works, but I need ot add more workbook options and Id rather not have multiple macros with the only difference being the variable for the second workbook to open and work with.
Its OK - tried the above and it seems to work fine - just lacked a bit of confidence I suppose.
I am using this line to copy some sheets between two workbooks
shtSummary.Copy after:=wbNew.Sheets(wbNew.Sheets.Count)
but it copys the equations not the values so it result to empty cells in wbNew Workbook
How can I copy the values only between them?
I don't think this is possible when copying sheets.
But you could try something like this:
Dim destWs As Worksheet
Dim AddressStr As String
AddressStr = shtSummary.UsedRange.Address
With wbNew
Set destWs = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With
destWs.Name = shtSummary.Name
destWs.Range(AddressStr).Value = shtSummary.Range(AddressStr).Value
I need to select columns on a specific sheet. Somehow this is not working:
Dim ws As Worksheet
Set ws = Worksheets("Mysheet")
ws.Columns("A:S").Select
Selection.EntireColumn.AutoFit
And simple Columns("A:S").Selectdoesn't activate the sheet I need
I tested your code and it works fine as follows.
Sub test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Mysheet")
ws.Columns("A:S").EntireColumn.AutoFit
End Sub
No need to Select anything, so I put the two statements together without the Select.
I added ThisWorkbook to (more) fully qualify your ws declaration. Make sure the worksheet Mysheet is in ThisWorkbook otherwise change that to state which workbook the sheet resides in.
Hi i am new for vba coding
Suppose there is a master sheet name "MDS" and there is a different workbook with different tabs like
Aon
Pge
Hilton
Hsbc
So i need to pull the data through vlook up in mastersheet of that particular tab
If i put aon it fetch aon data. Same goes with other client
Can you please help me with ths
If you have a Vlookup formula that references a different workbook, such as this:
=VLOOKUP(A1,[testOne.xlsx]Sheet1!$A$1:$B$5,2,0)
This would be the equivalent using VBA:
Sub UseVlookup_VBA()
Dim wb As Workbook, sh As Worksheet, rng As Range
Set wb = Workbooks("testOne.xlsx")
Set sh = wb.Sheets("Sheet1")
Set rng = sh.Range("A1:B5")
Range("C1") = Application.WorksheetFunction.VLookup(Range("A1"), rng, 2, 0)
End Sub