I have an Excel workbook that has been working but this morning have en-counted an error.
The error message I get is,
Run-time error 1004
Unable to set the ListFillRangeProperty of the DropDown class
Below is my code - the error happens at ws.DropDowns("DropDownStart").
DropDownStart and DropDownEnd are the correct names for the drop downs on my sheet so not sure what is causing this error. When I debug print the name I get
mysheetName!$A$2:$A$338
which is correct and all the cells contain dates in them. So bit stuck!
Dim ws As Worksheet
Dim wsTime As Worksheet
Set wsTime = ThisWorkbook.Sheets(WSTSJPM)
Set ws = ThisWorkbook.Sheets(WSCHARTS)
' get last date
Dim lRow As Long
lRow = wsTime.Range("A65536").End(xlUp).Row
ws.Select
ws.DropDowns("DropDownStart").ListFillRange = wsTime.Name & "!" & wsTime.Range("A2:A" & lRow).Address
ws.DropDowns("DropDownEnd").ListFillRange = wsTime.Name & "!" & wsTime.Range("A2:A" & lRow).Address
Update for comments
I can change the dropdown manually the code can change it normally too. The workbook is not being shared.
The sheet name contain no spaces in it.
I believe I should be using an "=" in the fill range unless you know another way?
The DropDown is a form control of type list box and is on my worksheet
Another problem looks like you have no quotes around the sheet names.
This code seems to work for me
Sub populatecombobox()
Dim ws As Worksheet
Dim wsTime As Worksheet
Dim lRow As Long
Set wsTime = Sheets("WSTSJPM")
Set ws = Sheets("WSCHARTS")
With wsTime
lRow = .Cells(Rows.Count, "A").End(xlUp).Row
End With
ws.DropDowns("DropDownStart").ListFillRange = "WSTSJPM!$A$2:$A$" & lRow
ws.DropDowns("DropDownEnd").ListFillRange = "WSTSJPM!$A$2:$A$" & lRow
End Sub
Related
I am trying to copy and paste all the data from one worksheet to a separate one in a completely different workbook. The code runs properly until the actual line trying to copy and paste the data: the error I get when running this line is:
"Run-time error '1004': Application or user-defined error."
Not sure if I even need the With statement or not...any help would be appreciated!
(Also I changed my file names just for privacy but they are complete in my actual code!)
Option Explicit
Sub btnCopyingData_Click()
' Copying data from one workbook to another
Dim fileDest As String, fileSource As String
Dim wbDest As Workbook, wbSource As Workbook
Dim wsDest As Worksheet, wsSource As Worksheet
Dim lrDest As Long, lrSource As Long
'The FILE that data is being copied TO
fileDest = "C:\Users\rest of path...Tar SAPCL Copy.xlsx"
'The WORKBOOK that data is being copied TO
Workbooks.Open Filename:=fileDest
Set wbDest = Workbooks("Tar SAPCL Copy.xlsx")
'The WORKSHEET that data is being copied TO
Set wsDest = wbDest.Worksheets(1)
'The ROW to which data will be pasted in the destination
lrDest = wsDest.Range("A" & wsDest.Rows.Count).End(xlUp).Row + 1
'---------------------------------------------------'
'The FILE that data is being copied FROM
fileSource = "C:\Users\Rest of path...SAPCL_20180720 Part 1.xlsx"
'The WORKBOOK that data is being copied FROM
Workbooks.Open Filename:=fileSource
Set wbSource = Workbooks("SAPCL_20180720 Part 1.xlsx")
'The WORKSHEET that data is being copied FROM
Set wsSource = wbSource.Worksheets(1)
'The LAST ROW of the data being copied
lrSource = wsSource.Range("A" & wsSource.Rows.Count)
With wsSource
wsSource.Range("A1:V" & lrSource).Copy Destination:=wsDest.Range("A1" &
lrDest)
End With
End Sub
The error is here:
With wsSource
wsSource.Range("A1:V" & lrSource).Copy Destination:=wsDest.Range("A1" & lrDest)
End With
In your code you are getting the value written on the last row of column A, which is usually an empty cell:
lrSource = wsSource.Range("A" & wsSource.Rows.Count)
Change it to the following:
lrSource = wsSource.Range("A" & Rows.Count).End(xlUp).Row
Some ideas how to get the last row:
EXCEL-VBA better way to find last row
http://www.cpearson.com/excel/LastCell.aspx
Then change this:
With wsSource
wsSource.Range("A1:V" & lrSource).Copy Destination:=wsDest.Range("A1" &
lrDest)
End With
to this:
With wsSource
.Range("A1:V" & lrSource).Copy Destination:=wsDest.Range("A1")
End With
Or depending on what exactly do you need, this could be ok as well:
With wsSource
.Range("A1:V" & lrSource).Copy Destination:=wsDest.Range("A" & lrDest)
End With
My lrSource variable line wasn't complete so it was being recorded as a value of 0 instead of the actual row integer value. I fixed this line and the code now runs.
I need an Excel worksheet that has data in every row to run a macro that creates a sheet for every row and has each sheet be created with a custom template I have saved on my computer.
So far, I have the macro working to open each row in a sheet but I do not know how to get the sheet to open with the custom template. Also I'd like to rename every sheet corresponding to a specific cell in each sheet.
The current code I am using to create a new sheet from each row is:
Sub RowToSheet()
Dim xRow As Long
Dim I As Long
With ActiveSheet
xRow = .Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To xRow
Worksheets.Add(, Sheets(Sheets.Count)).Name = "Row " & I
.Rows(I).Copy Sheets("Row " & I).Range("A1")
Next I
End With
End Sub
There is an annoying issue with the Excel object model here. You can't use Worksheets.Add and then use the Type parameter to specify the template. If you use Sheets.Add however, then it'll work.
Try this:
Dim NewSheet As Worksheet
Set NewSheet = Sheets.Add(After:=Sheets(Sheets.Count), Type:=TemplatePath)
NewSheet.Name = "Row " & I.Rows(I)
NewSheet.Copy Sheets("Row " & I).Range("A1")
Set the TemplatePath variable to a suitable path.
I have a list of items and a new sheet button like this:
For each item you have to be able to create new worksheet when you click on that button. It copies a worksheet from a template, one worksheet for a single row. Now i want to be able to copy some of the info from a row i select to that new worksheet cells when i click on the button and maybe rename the worksheet as a value in one of the cells (ID). The values i need are ID and name, maybe few more.
Sub AddNameNewSheet2()
Dim CurrentSheetName As String
CurrentSheetName = ActiveSheet.Name
'Add New Sheet
Dim i As Byte, sh As Worksheet
For i = 1 To 1
Sheets("Predlozak").Copy After:=Sheets("Predlozak")
Set sh = ActiveSheet
Next i
On Error Resume Next
'Get the new name
ActiveSheet.Name = InputBox("Name for new worksheet?")
'Keep asking for name if name is invalid - Here i want to change worksheet name to a specific cell value from selected row.
Do Until Err.Number = 0
Err.Clear
ActiveSheet.Name = InputBox("Try Again!" _
& vbCrLf & "Invalid Name or Name Already Exists" _
& vbCrLf & "Please name the New Sheet")
Loop
On Error GoTo 0
End Sub
Anyone has an idea how can i make this work?
Here's what I came up with, I also thought checkboxes would be a good way to pick the line, it's quite simple:
Sub AddNameNewSheet2()
Dim x As Long
Dim wks As Worksheet
Dim IdCell As Range, NamCell As Range, FormCell As Range
Set wks = ActiveSheet
Set IdCell = wks.Range("A:A").Find("TRUE").Offset(0, 1)
Set FormCell = IdCell.End(xlToRight)
Set NamCell = IdCell.Offset(0, 1)
Sheets.Add After:=Sheets(1), Type:= _
"C:\Users\*yournamehere*\AppData\Roaming\Microsoft\Templates\*yourtemplatehere*.xltm"
ActiveSheet.Name = NamCell
Dim wks2T As ListObject
Set wks2T = ActiveSheet.ListObjects(1)
With wks2T
.ListColumns(1).Range(2) = IdCell.Value
.ListColumns(2).Range(2).Value = NamCell.Value
.ListColumns(3).Range(2).Value = FormCell.Value
End With
End Sub
I made the template a table cause it is easier to specify exactly where to put stuff. And it is more manageable as more data is added in the future.
Sheet1 image
Sheet2 image
I am trying to work on a Worksheet whose name is a variable.
I have a main sheet, called "data" where I go to catch a list of names of existing sheets.
My code is as follows :
Dim data as Worksheet
dim sheet_name as String
Dim i as Integer
Set data = ThisWorkbook.Sheets("Data")
For i = 2 to 10
sheet_name = data.Range("A"&i).Value
With ThisWorkbook.Sheets(sheet_name)
'Operations on the worksheet
End With
Next i
The error prompted is "Runtime Error 9 : Subscript Out of Range" for the specific line :
With This Workbook.Sheets(sheet_name)
It is as if the object Sheets didn't understand the string sheet_name.
The Sheet "sheet_name" exists for sure, I double-checked.
Unfortunately, I cannot call the sheet by its name because I have too many sheets to operate on, this is why I wanted to do a loop.
I tried not working with the "With" clause but just referring to every object of the sheets with "ThisWorkbook.Sheets(sheet_name) in front but doesn't work either.
Do you know if it is possible to call a string variable inside a Sheets()?
Thanks a lot for your help !
Kind regards,
The reason for your error was given in the comments above by #chris neilsen
You could use the code below to check or avoid having these kind of errors:
Option Explicit
Sub CheckShtExists()
Dim data As Worksheet
Dim sheet_name As String
Set data = ThisWorkbook.Sheets("Data")
Dim ws As Worksheet
Dim ShtNamesArr() As String
Dim i As Long
ReDim ShtNamesArr(0 To ThisWorkbook.Worksheets.Count - 1) ' resize array to number of worksheets in This Workbook
' loop thourgh all worksheets and store their names in array
For Each ws In ThisWorkbook.Worksheets
ShtNamesArr(i) = ws.Name
i = i + 1
Next ws
For i = 2 To 10
If data.Range("A" & i).Value <> "" Then ' ignore blank cells
sheet_name = data.Range("A" & i).Value
If Not IsError(Application.Match(sheet_name, ShtNamesArr, 0)) Then ' use Application.Match to see there is a sheet with this name
With ThisWorkbook.Sheets(sheet_name)
'Operations on the worksheet
End With
Else ' No Match
MsgBox sheet_name & " doesn't exists in your workbook"
End If
End If
Next i
End Sub
I'm new to VBA and I've been trying to figure out how to open an input file and copy those contents to a hidden sheet on my workbook, I saw an example on how to do this but the number of rows and columns is static, and I have to use 3 different input files one of them that changes constantly so, although I've been trying to use my intuition on how to do this, I can't seem to find out the answer.
Here's it is:
Sub s()
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
Dim lastRow As Long
lastRow = sourceSheet.Range("A" & Rows.Count).End(xlUp).Row
Dim lastColumn As Long
lastColumn = sourceSheet.Cells(1, Columns.Count).End(xlToLeft).Column
targetSheet.Range("A1:A" & lastRow).Value = sourceSheet.Range("A1:A" & lastRow).Value
' Close customer workbook
customerWorkbook.Close
End Sub
I'm using EXCEL 2007
I apologize in advance if this is an stupid noob question, but I honestly give up don't know what else to do to make it work.
The problem Is I don't know how to make it select first to last row and first to last cell (non blank both: cells and rows)
Tried this:
targetSheet.Range("A1:A" & lastRow).End(xlUp).Value = sourceSheet.Range("A1:A" & lastRow).End(xlUp).Value
and this targetSheet.Range("A1:A" & lastRow).End(xlRight).Value = sourceSheet.Range("A1:A" & lastRow).End(xlRight).Value
What about something like this:
SourceSheet.UsedRange.Copy TargetSheet.Range("A1")