I have a working VBA script which currently replaces all dots by commas (posted below). However I would the script avoid the first two rows and change everything else. What would be the best and easiest way to do this? I have only found solutions where you have to set a range and but I want it to work for all excel sheets regardless of the number of rows or columns.
Sheets("Sheet1").Select
Cells.Replace what:=".", Replacement:=",", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
You can still use range, but generate it dynamically: use the macro recorder and see what it gives you if you select all cells with the Select All button at the top left corner, then hold down CTRL and unselect by clicking the row headers.
After having a selected range, you can use Application.Selection instead of Cells.
set wsCSV = ... (your csv worksheet)
set rng = wsCSV.UsedRange
set ws = ... (your worksheet)
ws.UsedRange.Offset(1).ClearContents()
ws.Range("A2").Resize(rng.Rows.Count, rng.Columns.Count).Values = rng.Values
Related
I have macros to dynamically create columns (via an "Add" button) in tables across multiple sheets in my workbook, in some cases on sheets containing multiple tables. I would like to also have a "Remove" button that finds all columns containing text "toolname" in all tables in the workbook and deletes them.
I've tried a few different loops but am not sure how to go about actually finding and deleting columns based on said text, whether it should be a structured reference or a range, etc. Pretty big vba noob here so any help in the right direction would definitely be appreciated!
Something like this to find what you are looking for ?
Dim rng As Range
Set rng = Cells.Find(What:="toolname", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not rng Is Nothing Then
'if no matches is found
End If
I have a code that allows me to follow a hyperlink to a separate sheet within the same workbook and filter such sheet by a value associated with my hyperlink. Note that I create the hyperlink first and outside of the VBA process. This is how I did it:
a) Open Visual Basic under Developer tab
b) Right-Click the workbook you want to add the macro to and click Insert >Module
c) Copy and paste the following code into the module:
Sub Filter(sCriteria As String)
lField = Cells(1, 1).EntireRow.Find("Isometric Number", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
Range("a1").AutoFilter Field:=lField, _
Criteria1:=sCriteria
End Sub
My understanding is that this module tells the workbook to autofilter the column named Isometric Number on the hyperlinked tab by a particular value that is called out below. You can change the column name to whatever you want, but it needs to be changed in the Module and on the tab where you want the autofilter to occur.
D) Copy and paste the following code to your main tab (expand Microsoft Excel Objects and double click your main tab - where the hyperlink and what I'm filtering by lives).
Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Call Filter(sCriteria:=Cells(Target.Parent.Row, 2).Value)
End Sub
This code determines the filter criteria for the module above to auto filter by. Right now it is set up to filter by the value in the cell in the same row as the hyperlink and in the 2nd column.
Now finally for the question: When my hyperlink refers to a separate workbook (instead of a sheet within the same workbook) I get the following error : Object variable or With block variable not set (Error 91). When I run the debug tool this is the line that gets highlighted:
lField = Cells(1, 1).EntireRow.Find("Isometric Number", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
I've done quite a bit of reading on this but I'm having difficulties figuring out how to read and understand the code. The closest thing to an answer has been this post: Can Excel vba invoke an autofilter when a hyperlink is clicked to open new workbook?
Try changing this line, as it will currently be referencing the the newly-opened workbook. You may need to change the sheet index (or name)
lField = ThisWorkbook.Sheets(1).Cells(1, 1).EntireRow.Find("Isometric Number", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
I am fairly new to excel and I am trying to setup a macro that adds a row after a specific point of the worksheet. Example: Row 2 contains text "Original", so it should insert a new row afterwards, and so on.
I know that it might be easier to insert before something, so I could change the setup (so for example the word "original" would be in row 2 and the new row is added above it) if that is an easier solution.
Is this possible? How?
Thanks for all the possible help.
Slightly simpler than the previous answer:
Sub NewRowInsert()
Dim SearchText As String
Dim GCell As Range
SearchText = "Original"
Set GCell = Cells.Find(SearchText).Offset(1)
GCell.EntireRow.Insert
End Sub
This will work with the current active sheet. If you want to use some other sheet, say Sheet2, you could use:
Set GCell = Worksheets("Sheet2").Cells.Find(SearchText).Offset(1)
and if you wanted to operate on a different workbook e.g. TestBook.xlsx, you could use:
Set GCell = Workbooks("TestBook.xlsx").Worksheets("Sheet2").Cells.Find(SearchText).Offset(1)
Note that I've avoided the use of select. This may not be an issue for you, but if you're searching through thousands of rows and making many replacements it could speed up your code considerably.
Try the code below = you can change the textvalue variable value to what ever you want to search for.
Sub newRow()
Dim textvalue As String
textvalue = "Original"
Cells.Find(What:=textvalue, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.offset(rowOffset:=1, columnOffset:=0).Activate
ActiveCell.EntireRow.Insert
End Sub
I want to replace a semi-colon with a comma in a range.
The following code only deletes the semi-colon, it doesn't put the comma in its place.
Range("AU2:AU250").Select
Selection.Replace What:=";", Replacement:=",", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Try this ...
Sub Comma()
Dim R As Range, C As Range
Set R = Range([AU2], [AU250])
For Each C In R.Cells
C = Replace(C, ";", ",")
Next C
End Sub
This is IMHO a more VBA like approach making use of range objects rather than simulating Excel stuff that you would use when interacting directly with your sheet
Can you copy paste some of the offending cells from the CSV range in the csv file? Open it in wordpad though, not in excel.
Also, what version of excel are you using? I haven't managed to get this to occur either on 2003 or 2010.
And I assume you need to do this more than once is why you are making a macro or is find/replace failing and you are posting macro code to get help with that too? :-)
I have a UserForm that sits on top of my spreadsheet and simply displays information from the row containing the currently selected cell. Buttons on the form allow the user to move up/down the spreadsheet row by row. For example, when the "next record" button is clicked, the code executed is something like:
Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
LoadValues
I would like it to work even if the user filters the data and then loads the form. However, using the above code, it wants to loop through all cells, not just the ones still visible after filtering. I've seen solutions for immediately looping through only visible cells, e.g.,
For Each viscell In rng.SpecialCells(xlCellTypeVisible)
...
Next viscell
So there seems like there should be a better, more direct way to do this than looping through all rows until I get to the next one with .Hidden = False but I can't seem to figure out how to get a reference to "the next visible cell" in order to select it.
Any pointers would be greatly appreciated. Thanks.
Here's a lightning-quick way to activate the first not-blank, visible cell in a filtered range. Just change 'Range("A1")' to whatever range you'd like to search after. Also consider whether you want xlByColumns or xlByRows.
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).Find(What:="*", _
After:=ActiveSheet.Range("A1"), LookIn:=xlFormulas, lookat:=xlPart, searchorder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
I recommend using tables whenever possible, as they have variable named ranges built-in that are easy to reference.
Here's another example, using tables. It's targeted to search a specific column, so it'll be even faster.
Just find and replace TABLE_NAME and COLUMN_NAME with your values.
ActiveSheet.Range("TABLE_NAME[[#All], COLUMN_NAME]]").SpecialCells(xlCellTypeVisible).Find _
(What:="*", After:=ActiveSheet.Range("TABLE_NAME[[#Headers],[COLUMN_NAME]]"), _
LookIn:=xlFormulas, lookat:=xlPart, searchorder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
No loops required because it's built off of Excel's "Find" functionality.
And just a tip,you can also use this to grab the data of such a cell on separate worksheet without activating/selecting it at all.
Just find and replace Dbl_EXAMPLE, SHEET_NAME, TABLE_NAME and COLUMN_NAME with your references.
Dim Dbl_EXAMPLE as Double
Dbl_EXAMPLE = Sheets("SHEET_NAME").Range("TABLE_NAME[[#All], COLUMN_NAME]]").SpecialCells(xlCellTypeVisible).Find _
(What:="*", After:=Sheets("SHEET_NAME").Range("TABLE_NAME[[#Headers],[COLUMN_NAME]]"), _
LookIn:=xlFormulas, lookat:=xlPart, searchorder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Value2
Just replace ".Value2" at the end with whatever property you're looking for.
Examples:
.Formula
.Row
.Column
The list goes on
I really struggled with this too and used Ryan's solution, albeit a little abbreviated, for my purposes..
This may be as brief as it gets. :)
ActiveCell.EntireColumn.SpecialCells(xlCellTypeVisible).Find(What:="*", After:=ActiveCell).Activate
Here is one method using a simple loop until the next row down is visible.
Dim rng As Range
Set rng = ActiveCell
Dim n As Long: n = 1
Do Until rng.Offset(n, 1).Rows.Hidden = False
n = n + 1
Loop
rng.Offset(n, 1).Select
LoadValues