I have a worksheet where the user can input a list of cell references as hardcoded values in cells. The VBA script is then supposed to take that input and work with the values in those exact cells in two separate worksheets. So for instance the input looks like the following:
Input
G29
H38
M92
The script is then supposed to loop through the range on input (in this case G29, H38 and M92) and go into a separate workbook (source workbook) where it then copies the values in those exact cells and then goes into another separate workbook (target workbook) and paste the values into the same cell references.
In the following code I have defined the variables as follows:
wsKpInput_source = The relevant worksheet in the source workbook
wsSCEInput_target = The relevant worksheet in the target workbook
Dim rng As Range: Set rng = Application.Range("Dashboard!E9:E11") 'This is the G29,H38,M92 input from the user
Dim cell_source As Range 'To take the references input by the user (G29,H38,M92) - unsure how to define this?
Dim cell_source_input As Variant 'To use the cell references in cell_source and be put equal to the content in that source workbook cell reference
For i = 1 To rng.Rows.Count
cell_source = rng.Cells(i, 1)
cell_source_input = wsKpInput_source.Range(cell_source)
wsKpInput_target.Range(cell_source) = cell_source_input
Next
Unfortunately this doesn't work as intended and I believe it is probably due to several issues? I would much appreciate any help with this.
This can be done properly on one single line as so:
Sub copy()
Dim rng As Range: Set rng = Application.Range("Dashboard!E9:E11") 'This is the G29,H38,M92 input from the user
Dim i As Integer
For i = 1 To rng.Rows.Count
wsKpInput_target.Range(rng.Cells(i, 1).Value).Value = wsKpInput_source.Range(rng.Cells(i, 1).Value).Value
Next
End Sub
The problem is that you tried .rng.cells(I,1) which will return a cell reference of that cell, where you needed the value within the cell as a cell reference (done with .value). Also copying from one cell to another can always be done with one line on two sides of an = statement.
Also this method is not the most efficient as it can probably done with adding all values to an array at one time, but that would probably take more time coding than you would save.
Related
My goal is to create a new worksheet and name it depending on a value in a drop down list and loop it for every "yes" in the list.
The last string is not working and I have no idea how to loop the renaming process to fetch the correct name.
For example: If cell I65 has the value of "AP" I want the newly generated sheet to be named to the value in A65.
Dim rng As Range, cell As Range, was As Worksheet
Set rng = Range("A1:A3")
Sheets("Setup").Select
For Each cell In Range("I48:I85")
If cell = "AP" Then
Sheets("AP").Select
Sheets("AP").Copy Before:=Sheets(1)
Dim AP(2) As Worksheet
Set AP(2) = ActiveWorkbook.Sheets("AP (2)")
AP(2).Name = Worksheets("Setup").Range("A48:A85").Value
End If
Next cell
Though I did not fully understand what you are trying to do, This statement looks suspicious:
AP(2).Name = Worksheets("Setup").Range("A48:A85").Value
you are trying to rename a worksheet with a range of values? -not allowed.
use a single cell like this or concatenate the values in the range before you remane.
AP(2).Name = Worksheets("Setup").Range("A48").Value
Have a look to the following code.
(1) Range("A48:A85").Value returns an array of values which cannot be used as a name. I have used the Offset-function to get the cell in col A of the same row.
(2) No need to use Select. Read (and understand) How to avoid using Select in Excel VBA
(3) Always qualify all worksheets, ranges and so on. With other words: Tell VBA which worksheet from which workbook you are using.
(4) I have used ActiveWorkbook in the With-clause, but I don't like it. Make up your mind on which workbook you are working. If it is the workbook the code lives, use ThisWorkbook instead. It it is another workbook, it's better to assign it to a workbook variable (eg when you open it). Especially in larger project, don't rely on Activeworkbook.
(5) You are declaring your variable AP as array with 2 elements. I guess you just wanted to name the variable similar to the new created sheet, but using an array and writing AP(2) is highly confusing. Just name it AP2 (or newWs or APCopy)
Dim ws As Worksheet, cell As Range
With ActiveWorkbook ' <-- Better change that.
Set ws = .Sheets("Setup")
For Each cell In ws.Range("I48:I85")
If cell = "AP" Then
.Sheets("AP").Copy Before:=.Sheets(1)
Dim AP2 As Worksheet
Set AP2 = .Sheets("AP (2)")
AP2.Name = cell.Offset(0, -8).Value
End If
Next cell
End With
I'm having some trouble trying to find VBA code to delete multiple specific cells if a certain cell contains a specific text. This spreadsheet can run close to 100k rows as well, but will vary depending on the data pull.
The specific VBA would be able to do the following:
If Cell J3 equals #N/A, Blank, or 0, then clear contents of cells J3:K3 and P3:X3, and then repeat til it reaches the bottom of column J.
Thanks in advance
How to clear contents for specified cells when another cell contains specific text or string
Dim cellToClear As Range
Dim cellToCheck As Range
Dim specificText As String
If cellToCheck.Value = specificText Then cellToClear.ClearContents
"I'm having some trouble trying to find VBA code "
These links contain VBA code that you can use when you no longer have trouble trying. They contain examples you can paste into your project and modify for your needs.
This link has examples of how to read the contents of a cell.
A range is a group of one or more cells in a worksheet. You can perform an operation on a range and it will affect all the cells inside the range. This link has examples of how to work with a range.
A loop is when the program repeats the same sequence of steps, usually until a specific condition is met. You can find examples of different loops here.
I prefer placing values into an array if you are going to be changing a bunch of cells in a routine. This generally makes the process much quicker.
Start out by setting your worksheet and range objects. Please take note that the below code is currently using index 1 for the worksheet here: Set ws = ThisWorkbook.Worksheets(1). If this is not the worksheet you are personally needing, then you will need to change this.
Then place the cell contents of the entire range into an array. As I mentioned earlier, this process is quicker than making adjustments to individual cells 1 at a time.
Loop the array, checking for either the specific error value #N/A or the other criteria. If this criteria is a match, you will enter another loop that quickly loops through the 'columns' in the row that will delete the values from only the columns you specified.
Once finished, rewrite the array back to the worksheet.
Sub main()
Dim ws As Worksheet, rng As Range, dataArr() As Variant
Set ws = ThisWorkbook.Worksheets(1)
Set rng = ws.Range("J3:X" & ws.Cells(ws.Rows.Count, "J").End(xlUp).Row)
' Place the entire contents of worksheet range into an array
dataArr = rng.Value
Dim i As Long, x As Long, clearRow As Boolean
For i = LBound(dataArr) To UBound(dataArr)
If IsError(dataArr(i, 1)) Then
If dataArr(i, 1) = CVErr(xlErrNA) Then clearRow = True
ElseIf dataArr(i, 1) = vbNullString Or dataArr(i, 1) = 0 Then
clearRow = True
End If
' Loop thru the columns (x) of the current row (i)
If clearRow Then
For x = 1 To 15
Select Case x
Case 1, 2, 7 To 15
dataArr(i, x) = ""
End Select
Next x
clearRow = False
End If
Next i
' Re-write the entire array back to the worksheet in one step
rng.Value = dataArr
End Sub
So I have a big excel sheet with a bunch of empty cells in various locations. I want an easy to work with list of which cells are empty. I was hoping to make a new worksheet that was populated with the locations of the empty cells. I wanted to have this to just populate the cells I want it to. I kept the header from the worksheet I will be checking and added a blank cells count, so I want the following cells in the column to be populated by the list of empty cell locations.
Now I know I can use =ISBLANK to test if a cell is empty or not, but I only care about the cells that return TRUE. So I figure I'll need a loop. And I want the location of the cell so I can use =CELL. And to make this most readable I want to do this on a column by column basis.
But I want to populate a spreadsheet with this information in a manner similar to how functions work (I just want to copy and paste it to other cells and columns). But it's pretty clear that I am going to need VBA.
My question is how can I create a macro to populate my spreadsheet with a list of empty cells? How do I apply it to the cells?
I assume you have data in sheet1, I have used sample range// Range("A1:c15") however you can define range as per need and blank cells address will be published in next sheet.
Sub FindBlank()
Dim rng As Range
dim i as long
For Each rng In Sheet1.Range("A1:c15").SpecialCells(xlCellTypeBlanks)
i = i + 1
Sheet2.Cells(i, 1) = rng.Address
Next
End Sub
If you want a list of the cells that are empty, you can use Range().SpecialCells(xlCellTypeBlank):
Sub getEmptyCellAddresses()
Dim rng As Range
Dim ws as Worksheet
Set ws = Sheets("Sheet1") ' CHANGE AS NECESSARY
Set rng = ws.Range("A1:A15").SpecialCells(xlCellTypeBlanks) ' Edit/change range as necessary
ws.Cells(1, 2).Value = rng.Cells.Address ' Change `ws.cells(1, 2)` to whatever destination you like
End Sub
Edit: Ah, beaten by 16 seconds by #RamAnuragi ...but anyways, they're slightly different ways to tackle the question so I'll leave it.
Edit: For funsies, here's another way to put them all in a column, one row per cell...and more, per your comments.
Sub listEmptyCells()
Dim emptyAddresses() As String
Dim i As Long
Dim ws As Worksheet
Dim rng As Range
Set ws = Sheets("Sheet1") ' CHANGE AS NECESSARY
Set rng = ws.Range("A1:A15")
If WorksheetFunction.CountBlank(rng) = 0 Then
MsgBox ("No empty cells in the range")
Exit Sub
End If
emptyAddresses() = Split(rng.SpecialCells(xlCellTypeBlanks).Address, ",")
For i = LBound(emptyAddresses) To UBound(emptyAddresses)
ws.Cells(i + 1, 2).Value = emptyAddresses(i)
Next i
End Sub
How can i modify the code below to select data from any worksheets and copy they to another worksheet for example select and copy data from Worksheets("uno") and paste they to Worksheets("duo"). Because the code below selects data only on activesheet
Set tbl = ActiveCell.CurrentRegion
tbl.Resize(tbl.Rows.Count, tbl.Columns.Count).Select
I have a code to copy data from any sheet to another for example
Worksheets("uno").Range("A5:T5,A7:T56,W5,Y5,W7:W56,Y7:Y56").Copy _
Worksheets("duo").Range("B4")
But i want to copy a range with data and ignore blank cells because the range A5:T5 it doesn't have always all cells with data concretely the last cells of this range, two or three of those, and also the same on range A7:T56.
My problem is how to select a range with data and ignore the blank cells inside the range A7:T56 concretely the last rows and the last columns which haves blank cells
Well, for the first part, where "the code selects data only on the activesheet", you just need to activate the correct sheet (for example: "Worksheets("uno").Activate") before executing "Set tbl = ActiveCell.CurrentRegion".
I am not really sure if I understand you correctly, but these are my thoughts:
If you don't want to activate worksheet "uno" you need to create a reference to that worksheet to have a direct access to it:
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Set wkb = Excel.Application.Workbooks("<name of your workbook>")
Set wks = wkb.Worksheets("uno")
If you now use the following code:
wks.Range("<your range>").Copy
you have just copied your selected cells, now you can paste it wherever you want.
As for the part with avoiding empty cells:
Generally speaking, you need to create a method of checking whether relevant cells are empty or not before you add them to your range.
Personally, I would avoid trying to copy the whole range as such. Instead I would:
1) loop through all relevant cells in your range one by one
2) for each cell check if it's empty
3) if empty, go to next cell
4) if not empty, copy that cell and paste to the target worksheet
5) jump to next relevant cell
6) when you reach the cell which is just after your last cell, quit looping
I would use the above defined wks object.
Note that a Range object can be treated as a collection of strings, so you can iterate using For... Next loop (For Each loop does not guarantee the index order).
Something like this should do:
Dim rng As Range
Set rng = wks.Range("<your range>")
Dim numOfItems As Integer, itm, i As Integer
numOfItems = rng.Count
For i = 1 To numOfItems
itm = rng.item(i)
If itm <> "" Then
'set value of the corresponding cell in your target worksheet to itm
'<relevant cell>.Value = itm
Else
'do nothing
End If
Next i
I hope it's at least a little bit helpful.
I currently copy a worksheet that contains workbook and worksheet-specific named ranges.
The worksheet-specific references need to be the way they are, the workbook ones need to be hardcoded.
I am looking for a way to fixate every named range that contains an external reference.
My current code looks like this, but it's not really what I am after:
Sub HardcodeValuesInExternalNamedRanges(wb As Workbook, ws As Worksheet)
Dim namCur As Name
For Each namCur In wb.Names
If (InStr(1, namCur.RefersTo, ThisWorkbook.Name) > 0) Then
namCur.RefersTo = "=" & ws.Evaluate(namCur.RefersTo)
End If
Next namCur
End Sub
I could not find a way to fixate the values properly. The way it is done now, does the job to some degree but I would prefer if the values were properly converted within the cell and not just on a named range basis.
Another option would be to loop through all cells and see if it contains a named range, but I think this would be too time consuming.
Is there a way to do it more efficiently?
Rather than loop through the cells, how about looping a .FindNext like this?
Do
rng.Value = rng.Value 'Hardcodes the value
Set rng = .FindNext(What:=ThisWorkbook.Name Lookin:=xlFormulas) 'finds the next Value to be hardcoded
Loop While Not Rng Is Nothing