VBA programming in Excel withOUT ActiveCell [closed] - vba

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Does anyone know if it is possible to loop through an Excel spreadsheet using VBA without using ActiveCell?
As an example, how can you create the COUNTIF function from scratch in VBA without using ActiveCell (or calling on the COUNTIF function, obviously)?
I want to avoid ActiveCell because it seems like an unnecessary use of resources to scroll the active cell around when typically you're trying to manipulate a simple matrix, especially when looping through thousands of cells.

Dim c as Range
For Each c in Sheets("Sheet1").Range("A1:A1000").Cells
'do something with c
Next c

What Tim said.
Just to address the count-if portion of your question, here is a way to do it without using the formula:
Sub Macro14()
Dim c As Range
Dim rng As Range
Dim count_if As Integer
Set rng = Sheets("Sheet1").Range("A1:A1000")
For Each c In rng
If c = "Apple" Then
count_if = count_if + 1
End If
Next c
Debug.Print count_if
End Sub

Related

VBA Do While Loop Excel Macro [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to create a macro that when a button named "Run Formula" is clicked, it copies a specific block of cells starting from I12, then moves down 28 rows and pastes the formulas from the block of cells in I40. It then loops to do this 150 times.
Note: I did make this work by recording a Macro and copy and pasting 150 times but I'd like to edit the code to be simpler.
I have never used VBA before but this is what I have so far:
Sub Button6_Click()
Button6_Click Macro
Range("I12:AG22").Select
Selection.Copy
Dim i As Integer
i = 0
Do While i < 150
Row 40 + i * 28
i = i + 1
Loop
End Sub
use a for loop it will save some typing.
Also avoid the use of .Select or .Activate
Also get into the habit of always assigning the parent sheet to any Range or Cells object.
Sub Button6_Click()
'Button6_Click Macro
Dim i As Long
With Worksheets("Sheet1") 'Change to the name of the sheet
.Range("I12:AG22").Copy
For i = 0 to 149
.Cells(40 + i * 28,"I").PasteSpecial xlPasteFormulas
Next i
End With
End Sub

Excel VBA How to multiply range by one number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am very new to VBA and I am seeking help to solve the following problem. I need to multiply a range by a single number. All of the cells with in that range need to be multiplied by that one number.
Thank you!
As what I comment just now.
1.Enter the formula at the cells (e.g. G1)
2.Press Enter key and drag the formula
keong has already shown you one method but unfortunately that method requires one to do the calculation in another cell/range. If that is what you want then go with keong's answer but if you want to do the calculation in the same range then continue reading below.
Here is another method which doesn't use formulas or VBA.
Let's say the range is A1:A10 and you want to multiply the entire range by 5
Simply type 5 in any blank cell. You can delete that later.
Copy that cell
Select Range A1:A10
Right click on it
Click on Paste Special | Values - Multiply as shown below and you are done.
Before
After
Followup from comments
In case you do not want to use a temp cell to write 5 then you can directly set 5 in the clipboard and use it like this.
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim MyData As New DataObject
Dim rng As Range
Set ws = Sheet1
Set rng = ws.Range("A1:A5")
'~~> Put the number 5 in clipboard
MyData.SetText 5
MyData.PutInClipboard
'~~> Get the data from clipboard
MyData.GetFromClipboard
rng.Formula = Application.Evaluate("=" & _
rng.Address & _
"*" & _
Val(MyData.GetText))
End Sub
Like I mentioned, you don't need VBA for this but if you still want to use VBA then you can use this instead of copying the data to the clipboard.
rng.Formula = Application.Evaluate("=" & rng.Address & "*" & MYNUMBER)
Where MYNUMBER is the variable which has the number that you want to multiply the range with.

Conditionally copy rows to different sheet if leftmost cell has ANY value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
New to the site; would greatly appreciate your help!
I want to conditionally copy rows from "sheet1" to "sheet2".
Condition: The leftmost cell in the row (column "A") has ANY value (numbers or text).
*I do not want to copy rows that are blank.
I'd like to run the VBA macro for all of sheet1(specifically columns A through L and rows 10 - 9999).
If you guys need any more info, please let me know; Thanks in advance!!
Without writing your code for you, this will get you started:
Private Sub Find_Non_Blanks()
Dim cell As Range, rowOut As Long
rowOut = 0
For Each cell In Sheets("Sheet1").Range("A10", "A9999")
If Not IsEmpty(cell) Then
rowOut = rowOut + 1
Debug.Print rowOut & ":", "A" & cell.Row, cell.Value, cell.Formula
End If
Next
Debug.Print "Found " & rowOut & " rows to be copied."
End Sub
You can put this code on any sheet, userform or module. It will produce the same output regardless of which sheet is currently active, or what the current selection is.
For speed, reliability and ease-of-maintenance, your VBA should operate directly on ranges without selecting or activating them. In other words, try hard not to use these keywords in your VBA code:
Select, Selection, Activate, ActiveCell

How to search for an item and select it in VBA [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to search for a value in a cell in a spreadsheet and select it. Here is some pseudocode for what I want to do:
search for the unique value "Justin123" in spreadsheet
select the cell that contains the value "Justin123"
offset 2 cells to the left of the searched value and replace with new value "John123"
I tried to google a solution but all I found were recommendations on using the record macro feature to search and replace an item. However, what I need to do is slightly different since I need to search and replace the item two units to the left of the searched cell.
See if you understand this code (hope it gets you interested in learning VBA):
Sub TestReplace()
ActiveSheet.Cells.Find(What:="Justin123", LookAt:=xlPart).Offset(0,-2).Value = "John123"
End Sub
The Macro Recorder is a great tool for beginners. Please do use it for simple cells operations.
Basically, what you have to do is initialise a RANGE object (which represents a cell in excel). Then you set the range object to the output of the function .Find(), which returns a range object from the sheet that matches the search inputs. If there is no match, it sets the range object to Nothing. You can then use the method .Offset() that range objects have to traverse across the other range objects in the sheet.
Sub Main()
Dim rng As Range
Set rng = ActiveSheet.Cells.Find(What:= "Justin123", LookAt:= xlPart)
' Check if rng is not Nothing [ie a match was made using .Find()]
If Not rng Is Nothing Then
rng.Offset(0, -2).Value = "John123"
End If
End Sub

How to hide columns (G-AZ) when cell value is #N/A [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I currently have a vlookup populating row 1 (cells G1-AZ1) with titles, and would like to hide the Columns(G1-AZ1) if the vlookup pulls back nothing/#N/A. I know this is a simple macro but I'am new to VBA and I have had zero luck searching the web.
Thanks!
I usually place such formulas in ISNA() and then just use Excel filter to hide empty rows
=IF(ISNA(VLOOKUP(A3,G1:H7,2,FALSE)),"",VLOOKUP(A3,G1:H7,2,FALSE))
Try this:
Loop throught he header cells
Set the EntireColumn.Hidden property based on your criteria
Use .ScreenUpdating = False to prevent screen flicker and speed it up
Sub HideColumns()
Dim rng As Range
Dim cl As Range
Application.ScreenUpdating = False
Set rng = [G1:AZ1]
For Each cl In rng
If IsError(cl) Then
cl.EntireColumn.Hidden = cl = CVErr(xlErrNA)
Else
cl.EntireColumn.Hidden = cl = ""
End If
Next
Application.ScreenUpdating = True
End Sub