How do I limit a Macro to function only within certain range let's say until certain column?
I have a case where macro needs to delete the rows based on condition. I also want to put some instructions on the right and a form control button for macro but it seems like every time the code is activated, it deletes everything in that row, including the instructions and button I made. So I want to define a range where macro should be active.
I have following logic active.
First flag the item to be checked.
In loop, keep checking and deleting the rows.
Delete the rows based on an extra condition.
The code is working fine, but I want to add limitation until certain column like said.
Where do i define the range? In three separate logic, or at once on top?
Any help would be appreciated.
BR,
Manny
It sounds like you have told your code to delete the entire row. If you only want the macro to delete the data in columns A to G, for example, then use code along the lines of
Range("A" & rownumber & ":G" & rownumber).Delete Shift:=xlUp
you need to replace the line of code that deletes the whole row with code that deletes only set range. So for example instead of
Rows("12:12").Delete Shift:=xlUp
you will have
Range("A12:N12").Delete Shift:=xlUp
Related
As the title says, It seems obvious but I can't seem to find a one-liner to it without looping. Is there a method able to do this task with a fast execution time ?
Here is my failed attempt (It crashes my excel file) :
Worksheets("Iso_Journal").Range("A:P").EntireRow.SpecialCells(xlBlanks).EntireRow.Delete
Any knowledge would be appreciated !
Edit:
Found this method but it filters instead of deleting (much faster), it does the job but it still not what I truly need
Worksheets("Iso_Journal").Range("A:P").AutoFilter Field:=1, Criteria1:="<>"
Edit 2: (Response to #FaneDuru)
Please, try the next adapted code. If the target is to delete all empty ROWS, it is enough to use a single column. And also to limit the number of rows to be processed:
Dim wsJ As Worksheet
Set wsJ = Worksheets("Iso_Journal")
wsJ.Range("A1:A" & wsj.Range("A" & wsJ.rows.count).End(xlup).row).SpecialCells(xlBlanks).EntireRow.Delete
If the range to be processed is huge, and the workbook contains many formulas, you can make the code a little faster placing Application.Calculation = xlCalculationManual before the deletion line, followed by Application.Calculation = xlCalculationAutomatic. Even if the rows are deleted in block, Excel needs to update the formulas row by row, and no good to calculate after each...
I'm trying to get some details copied in Excel from Sheet 1 columns 1-5 to Sheet 2 columns 1-4, but only for lines that include text or values on sheet one under a specific column (in this case, Column 2). There are other columns in between, so I need to be able to use exact columns rather than A:D for example.
Example of what I'm trying to achieve:
I have tried using a simple IF function with A:A<>"" so it would include any rows that have any data in them, however this does not seem to copy as I need and occasionally based on my attempts i also get circular reference errors. Additionally, I’m not sure how to make sure this gets pasted at the bottom of a table that will expand with each addition.
I realize a probably easier option would be to simply copy Sheet 1 entirely and use a filter on row 1 to deselect Blanks on A:A, but the sheet has so much more info that it would be a waste, and additionally info is constantly added so I need something scale-able. It also occurred to me now that by doing this i would include info from the "header" and "footer", basically a frozen pane - which I do not need.
Could this be done via a simple function, or would it require a Macro?
Please keep in mind I'm rubbish at programming, just trying to make my life easier and learn as I go. A lot of excel forums help but still I'm no coder. I can understand to a pretty big degree what the code does and can adjust accordingly though :)
As suggested, this cannot be done with formulas. There are different ways to achieve this.. below is one approach:
Sub CopyFilteredRows()
Dim oSourceSheet As Worksheet: Set oSourceSheet = ThisWorkbook.Worksheets("Sheet3") ' Set your source sheet here
Dim oRng As Range: Set oRng = oSourceSheet.Range("A2:E" & oSourceSheet.Range("C" & oSourceSheet.Rows.Count).End(xlUp).Row)
' Set filter on column B
oRng.AutoFilter
oRng.AutoFilter 2, "<>"
' Copy to specified sheet
oRng.SpecialCells(xlCellTypeVisible).EntireRow.Copy ThisWorkbook.Worksheets("Sheet4").Range("A2") ' Change your destination sheet here
' Clear objects
Set oRng = Nothing
Set oSourceSheet = Nothing
End Sub
Paste the above UDF in a Module and then run it whenever you want to perform the copy. I suspect you would have to modify it a bit so that you can cater for your particular scenario but it should give you a start
Trying to delete, rather than hide from one worksheet (where the filter button is to another where the catalogue list is. i.e. trying to create something like (best way to describe this) a shopping basket based upon the users selection and reducing the (long) list on the other worksheet after selection.
I am beginning to pull my hair out on this one and after having read and watched many, many articles on deleting rows after filtering on empty cells and today nothing has really helped as there are more issues than solutions when following codes from other, thus far.
Basically, I have a Hugh catalogue on a separate worksheet and if the user says yes, this should be shown and if not if blank (but with a value) it should be deleted. See so very basic script I have that works perfectly, but I have to delete and not hide. It appears EntireRow.delete is something beyond me, as it introduces many many issues, where hide simply worked so smoothly.
Trying many other scripts, they all really fail in simplifying the answer and 99% are actually for a single worksheet and range rather than a specific worksheet and specific columns i.e. E:E (script script below shows more) I am using a table too, so this is a little different too.
For a = 2 To 150
If Worksheets("Requirements").Cells(a, 5).Value = "High" Then
Worksheets("Requirements").Rows(a).Hidden = True
End If
Next
Anyone with a brilliant one or two liner to delete rather than hide, or delete all hidden if necessary
Many thanks in advance
Consider:
Sub sjdhfs()
For a = 150 To 2 Step -1
If Worksheets("Requirements").Cells(a, 5).Value = "High" Then
Worksheets("Requirements").Cells(a, 5).EntireRow.Delete
End If
Next a
End Sub
Note we run the loop from the bottom to the top.
I'm very new to this. However, I've created a small VBA script in Excel. This script highlights the row the button is on, asks for a name and then inserts today's date and the name entered on to some cells in the row. It's basically used to book out media that is recorded on this spreadsheet.
The issue I have is that I'll need a CommandButton for every row, and I have no idea how to get the command button to change the the row values within the script to reflect the row that the button is on. Is this possible?
I think you should specify your question more clearly, but if I understand your question right, you need to write data in the next empty cell. If that is the case, you can use this:
ActiveSheet.Range("B10").Select
If ActiveCell.Offset(1, 0) <> "" Then
Selection.End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
This will search for and select the next empty cell in column B, starting from B10. Hope this helps!
I have to clear #N/A that happens through out my worksheet when ever i run my code. I'm not sure why and have been debugging for a while but to no avail. What i could do to remedy this problem is to delete it entirely from the page, where they happens randomly. If anyone know how to, do share a VBA code with me.
Codes of doing a simple copy and paste into another sheet
thevaluestocopy = Sheets("pivot").Cells(thefirstrow, 1) _
.Resize(thelastrow - thefirstrow, 1)
Worksheets("summary").Cells(3, 16) _
.Resize(thelastrow - thefirstrow + 1, 16) = thevaluestocopy
I have nested that code with different Column because my pivot table changes most of the time. And when i copied for the 2nd time, #N/A appears.. Have no idea why and i believe this works work fine.
You can run this line to remove any #N/A error values in the worksheet:
Cells.Replace "#N/A","",xlWhole
If the worksheet contains formulas you can try this:
Cells.SpecialCells(xlCellTypeFormulas,xlErrors).Clear
I don't know, but how about doing something like this instead? This is how I generally copy and paste, without problems.
Sheets("pivot").Range("your range").Copy
Worksheets("summary").Range("your range").PasteSpecial
UPDATE
If you still want to simply remove all the #N/As with your current code, you can use some code like this.
If WorksheetFunction.IsNA(Cells(row, column)) Then Cells(row, column).ClearContents