Copy worksheet and rename it and declare variable vBA - vba

I am copying a worksheet and rename it using the cell values from 3rd sheet. THe only issue I am having is how do I declare the new sheet a a variable since i will be working on that sheet? I get an error saying "expected: end of statement" on the the last line.
Dim wsNew As Worksheet
Dim wsIntro As Worksheet
Dim wsUp As Worksheet
Set wsUp = Worksheets("Sheet1")
Set wsIntro = Worksheets("Instructions")
Worksheets("Sheet1").Copy after:=Sheets(Worksheets.Count)
With ActiveSheet.UsedRange
.Value = .Value
End With
ActiveSheet.name = wsIntro.Range("b6").Value & wsIntro.Range("b7").Value
Dim wsAllo As Worksheet
Set wsAllo = "wsIntro.Range("b6").Value & wsIntro.Range("b7").Value"

As the worksheet that you are trying to set a reference to is the ActiveSheet, you can simply change
Set wsAllo = "wsIntro.Range("b6").Value & wsIntro.Range("b7").Value"
to
Set wsAllo = ActiveSheet
Refactoring your code slightly gives:
Dim wsNew As Worksheet
Dim wsIntro As Worksheet
Dim wsUp As Worksheet
Dim wsAllo As Worksheet
Set wsUp = Worksheets("Sheet1")
Set wsIntro = Worksheets("Instructions")
'You shouldn't use "Sheets(Worksheets.Count)" - it will sometimes not do
'what you expect (when you have Charts as well as Worksheets in the Workbook)
'Use either "Sheets(Sheets.Count)" to place the new sheet as the last sheet
'in the workbook or use "Worksheets(Worksheets.Count)" to place the new sheet
'after the last worksheet in the workbook (but possibly with Charts after that)
wsUp.Copy After:=Sheets(Sheets.Count)
Set wsAllo = ActiveSheet
With wsAllo
.Name = wsIntro.Range("b6").Value & wsIntro.Range("b7").Value
.UsedRange.Value = .UsedRange.Value
End With

Related

copying range end xl down pasting different Wb

I've got a macro recorder code (with select and activate) that I'm trying to simplify. It currently looks like this:
Windows("Stambestand.xlsm").Activate
Range("AA2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Ijking document.xlsm").Activate
Range("B3").Select
ActiveSheet.Paste
As of recently I've started using variables and want to trim down the code. I'm thinking along these lines:
WbStambestand.WsStam.Range("AA2", Selection, Selection.End(xlDown)).Copy WbIjk.WsIjk.range("A1").paste
Workbooks("WbStambestand").Worksheets("WsStam").Range("AA2", Selection, Selection.End(xlDown)).Copy
However, these don't function. I'm hoping you guys can help me along. Much appreciated.
FYI, these are my variables (they are declared). The Ijk ones are the paste destination.
Dim WbStambestand, WbIjk As Workbook
Dim WsIjk, WsStam As Worksheet
Set WsIjk = ActiveSheet
Set WbIjk = ActiveWorkbook
Set WsIjk = ActiveSheet
Set WbIjk = ActiveWorkbook
Set WbStambestand = Workbooks.Open(stam)
Set WsStam = WbStambestand.Worksheets("stambestand")
Try this
Sub abc()
Dim WbStambestand As Workbook
Dim WbIjk As Workbook
Dim WsIjk As Worksheet
Dim WsStam As Worksheet
Dim LastRow As Long
Dim stam As String
stam = "C:\Users\Admin\Desktop\Stambestand.xlsm" ' path with complete file name with extension.
Set WsIjk = ActiveSheet
Set WbIjk = ThisWorkbook ' workbook which has current code
Set WbStambestand = Workbooks.Open(stam)
Set WsStam = WbStambestand.Worksheets("stambestand")
LastRow = WsStam.Range("AA2").End(xlDown).Row
WsStam.Range("AA2:AA" & LastRow).Copy WsIjk.Range("B3")
End Sub

1004 error for using usedrange

I am trying to copy over values from one workbook to another using the usedrange function (there are some blanks in certain rows and it is fine to copy over the blank as well), but I am getting the 1004 error:
Sub ActiveInactiveVendors()
Dim ActiveWkb As Workbook, Wkb As Workbook, InactiveWkb As Workbook
Dim ActiveWkst As Worksheet, Wkst As Worksheet, InactiveWkst As Worksheet
Dim aCell As Range
Dim targetRng As Range
Set ActiveWkb = Workbooks.Open("C:\Users\clara\Desktop\active vendors.xlsx")
Set Wkb = ThisWorkbook
Set Wkst = Wkb.Sheets("Vendors")
Set ActiveWkst = ActiveWkb.Worksheets("aqlc7da48e7")
'set column A starting from A7
Set targetRng = Wkst.Range("A7" & Wkst.Rows.Count)
'get the values starting from a32 to the last row used and set it in wkst
targetRng.Value = ActiveWkst.Range("A32" & ActiveWkst.UsedRange.Rows.Count).Value
End Sub
I appreciate any feedback! Thank you
This line:
Set targetRng = Wkst.Range("A7" & Wkst.Rows.Count)
is not correct. You are not getting A7:A1048576 but a concatenating of the two. So it is looking for A71048576 which does not exist
Then you should use similar sized ranges when setting values.
Sub ActiveInactiveVendors()
Dim ActiveWkb As Workbook, Wkb As Workbook, InactiveWkb As Workbook
Dim ActiveWkst As Worksheet, Wkst As Worksheet, InactiveWkst As Worksheet
Dim aCell As Range
Dim targetRng As Range, origRng As Range
Set ActiveWkb = Workbooks.Open("C:\Users\clara\Desktop\active vendors.xlsx")
Set Wkb = ThisWorkbook
Set Wkst = Wkb.Sheets("Vendors")
Set ActiveWkst = ActiveWkb.Worksheets("aqlc7da48e7")
'set column A starting from A7
Set origRng = ActiveWkst.Range("A32", ActiveWkst.Cells(ActiveWkst.Rows.Count, 1).End(xlUp))
Set targetRng = Wkst.Range("A7", Wkst.Cells(origRng.Rows.Count + 6, 1))
'get the values starting from a32 to the last row used and set it in wkst
targetRng.Value = origRng.Value
End Sub

Copy specific entire column from file 1 to 2

Hello I'm trying to copy columns C, R, W,X from file 1 to file 2 with below code but keep getting an error. My VBA knowledge isn't that good yet but probably has to do with the range setting? I've tried multiple ways but can't get it to work.
Am I using the right setting or should I use another action to get the specific columns?
Sub PFS()
Dim wbCopy As Workbook
Dim wsCopy As Worksheet
Dim rngCopy As Range
Dim wbPaste As Workbook
Dim wsPaste As Worksheet
Dim rngPaste As Range
Set wbPaste = ActiveWorkbook
Set wbCopy = Workbooks.Open("path to copy")
Set wsCopy = wbCopy.Worksheets("Blad1")
Set rngCopy = wsCopy.Range("d, e").EntireColumn
Set wsPaste = wbPaste.Worksheets("PFS")
Set rngPaste = wsPaste.Range("a1")
rngCopy.Copy
rngPaste.PasteSpecial
Workbooks.Application.CutCopyMode = False
Application.DisplayAlerts = False
wbCopy.Save
wbCopy.Close
End Sub
Solutions to copy entire column.
Sub copy()
Dim wb As Workbook
Dim wbNew As Workbook
Dim ws As Worksheet
Dim wsNew As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.Sheets("old")
Set wbNew = Workbooks("Book.xlsx")
Set wsNew = wbNew.Sheets("new")
ws.Columns(3).copy
wsNew.Columns(3).Insert Shift:=xlToRight
ws.Columns(18).copy
wsNew.Columns(18).Insert Shift:=xlToRight
ws.Columns(23).copy
wsNew.Columns(23).Insert Shift:=xlToRight
ws.Columns(24).copy
wsNew.Columns(24).Insert Shift:=xlToRight
Set wsNew = Nothing
Set wbNew = Nothing
Set ws = Nothing
Set wb = Nothing
End Sub

Excel Macro to name ranges according to the name of the worksheet Tab name

I have the following code which I am trying to get to name the entire A and B columns range according to the worksheet tab name. I want each A:B range of cells in each worksheet to be named RoomCode_ + the name of the excel sheet tab.
So for example if I had 3 sheets called XYZ, ABC and DEF, then my cell range names for those 3 sheets respectively should be:
RoomCode_XYZ
RoomCode_ABC
RoomCode_DEF
I would typically do this manually by highlighting the cell range and just typing the range name I wanted, however I have over 150 tabs and would like to be able to do them all automatically through this process.
Sub nameRanges()
Set wbook = ActiveWorkbook
For Each sht In wbook.Worksheets
sht.Activate
RangeName = "RoomCode_" + ActiveSheet.Name
CellName = "A:B"
Set cell = ActiveWorksheets.Range(CellName)
ActiveWorksheets.Names.Add Name:=RangeName, RefersTo:=cell
Next sht
End Sub
Just a bit of refactoring to get what you need. Biggest this to work directly with objects and eliminate the Active... stuff.
Also ActiveWorksheets is not proper syntax in any way.
Sub nameRanges()
Dim wbook As Workbook
Set wbook = ThisWorkbook
Dim sht As Worksheet
For Each sht In wbook.Worksheets
Dim RangeName As String, CellName As String
RangeName = "RoomCode_" + sht.Name
CellName = "A:B"
Dim cell As Range
Set cell = sht.Range(CellName)
sht.Names.Add Name:=RangeName, RefersTo:=cell
Next sht
End Sub
Here's another way:
Option Explicit
Sub nameRanges()
Dim sht As Worksheet
Dim RangeName As String
Dim cell As String
For Each sht In ActiveWorkbook.Worksheets
RangeName = "RoomCode_" + sht.Name
cell = "=" & sht.Name & "!" & "A:B"
Names.Add Name:=RangeName, RefersTo:=cell
Next sht
End Sub
I think that you would want to add the names to the workbook names collection. The way it is now you'll still have to reference the individual worksheet before you can access the name.
WorkSheets("RoomCode").Range("RoomCode_XYZ")
By adding the names to the workbook you'll be able to access no matter the ActiveSheet.
Range("RoomCode_XYZ")
Sub nameRanges()
Dim wbook As Workbook
Set wbook = ThisWorkbook
Dim sht As Worksheet
For Each sht In wbook.Worksheets
Dim RangeName As String, CellName As String
RangeName = "RoomCode_" + sht.Name
CellName = "A:B"
Dim cell As Range
Set cell = sht.Range(CellName)
ThisWorkBook.Names.Add Name:=RangeName, RefersTo:=cell
Next sht
End Sub

dynamically selecting workbook and worksheet

I have to copy and paste specific columns from one worksheet in workbook1 to another worksheet in the same workbook1 or it may be workbook2 also. I mean I want to dynamically select the source workbook and worksheet and also the destination workbook and worksheet. I must be able to select the columns that I want to copy also dynamically.
I've tried this:
Dim thisWb As Workbook
Dim destWb As String
Dim destSheet As Worksheet, FromSheet As Worksheet
Dim FromBook As String
Set thisWb = ThisWorkbook
Set destSheet = thisWb.ActiveSheet
FromBook = Application.GetOpenFilename
If FromBook = "False" Then Exit Sub
destWb = Application.GetOpenFilename
Workbooks.Open Filename:=destWb
Set FromSheet = destWb.Worksheets("Sheet1")
Set sourcecolumn = Workbooks("FromBook").Worksheets("sheet1").Columns("A")
Set targetcolumn = Workbooks("destWb").Worksheets("sheet2").Columns("B")
sourcecolumn.Copy Destination:=targetcolumn
There is an "invalid specifier" compile time error and destwb is highlighted on this line:
Set FromSheet = destwb.Worksheets("Sheet1")
i have tried doing this with static workbooks,worksheets,column names and it works.
Dim sourcecolumn As Range, targetcolumn As Range
Set sourcecolumn = Workbooks("Book1.xlsm").Worksheets("sheet1").Columns("A")
Set targetcolumn = Workbooks("Book1.xlsm").Worksheets("sheet2").Columns("B")
sourcecolumn.Copy Destination:=targetcolumn
The problem is i want to select the workbooks,worksheets and columns dynamically...
There is some type confusion in your variable declarations. On the line on which you get the error, you're trying to assign a Worbook object reference to a String variable. This doesn't work because those are two different data types.
Here's a fix. I commented the lines that I changed:
Dim pthFromBook As String
Dim pthDestWb As String ' to store the path of the workbook file
Dim thisWb As Workbook
Dim destWb As Workbook ' to store reference to workbook object
Dim destSheet As Worksheet, FromSheet As Worksheet
Set thisWb = ThisWorkbook
Set destSheet = thisWb.ActiveSheet
pthFromBook = Application.GetOpenFilename
If pthFromBook = "False" Then Exit Sub
pthDestWb = Application.GetOpenFilename ' first get the path
Set destWb = Workbooks.Open(pthDestWb) ' then open the workbook
Set FromSheet = destWb.Worksheets("Sheet1")
Finally, I don't know if it's a typo, but on the last line above, there appears to be some confusion between From and Dest sheets/workbooks...