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.
Related
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 4 years ago.
Improve this question
I am new to VBA, so I have trouble writing code in VBA. My problem is writing a code or an Excel formula to extract (for later use) the identifiers that are written in the same cell, split by ";".
For example, this is what I have on a cell:
A11;B22;C33
I need to select A11, B22, C33, as they were written for example, on a column, as different cells, so I can later use them in other formulas.
I don't want to split the content into different columns, I just want to be able to use A11 or B11 furthermore, without editing it in the databse
Thank you!
you can use Split function and pass delimiter (;in your case)
for Example:
Var arrayResult = Split("A11;B22;C33",";")
you can also use text to column utility in excel.
select the text you want to separate
go to DATA >
select text to column>
select delimited option click Next>
select your favorite delimiter ;
click next
you will get the separate values.
I believe the following should work as you expect it to, simply amend the worksheet you are using, the cell you are referring to and the Column you wish your results to be placed in:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set the worksheet you are working with
Dim newvar As Variant, var As String
var = ws.Range("A1").Value
'get the contents of your desired cell, in this case A1
newvar = Split(var, ";") 'split the cell contents by delimiter ; into an array
For i = LBound(newvar) To UBound(newvar) 'loop through array to place values into column B
ws.Cells(i + 1, "B").Value = newvar(i)
Next i
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
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 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
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