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
Related
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.
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
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 have no prior experience with VBA/Visual Basic/Macros and all that - but I need to create a button that cause a cell to display another random cell's data.
I have a word list of several hundred cells and need a button that will choose one of them to be displayed in a cell at the top of my worksheet. The button needs to be able to be pressed multiple times and still bring up a random word. It doesn't need to have a fail-safe that prevents the same cell being displayed twice in a row.
I don't care how it is accomplished - it just needs to be explained a bit more as I have no idea how VBA works.
I'll give you a starter to assist your learning - (it is Christmas!) and you have the job of researching how to modify it and apply it. To get you going put this code in a standard code module, adjust the various variables to suit your display/data requirements, add a shape to the spreadsheet and assign this macro to that shape. My word list is on worksheet "Sheet1"
Sub showRandomWord()
Dim ws As Worksheet
Dim stRow As Long, endRow As Long, dataCol As Long
Dim dispRow As Long, dispCol As Long
Set ws = Sheets("Sheet1")
stRow = 2
dataCol = 3
dispRow = 2
dispCol = 4
With ws
endRow = .Cells(.Rows.Count, dataCol).End(xlUp).Row
.Cells(dispRow, dispCol).Value = _
.Cells(Application.RandBetween(stRow, endRow), dataCol).Value
End With
End Sub
Depending on your exact needs you might not even need VBA,
say you assign the name "data" to the range with the list
in the cell where you want to show the random value put the formula
=INDEX(data,INT(RAND()*COUNTA(data))+1)
Then a new random value will be shown each time the sheet is recalculated, you can do this manualy by pressing F9 key
If an actual button is required then use VBA , in the buttons event :
dim l as long
l=Sheet1.Range("data").count
Sheet1.Range("result")= Sheet1.Range("data").cells(fix((rnd*l)+1),1)
here i assume the list is in a range named "data", and the cell where you want the result is named "result"
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
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