Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a task at hand. I want to create a summary sheet by extracting data from multiple workbooks in a folder. These files are same in formatting. What i need to do is extract data from each workbook from the 5th worksheet. The cells are G4, H4, G8, H8, G10, H10, G17, H17. I found a code at microsoft.com.
The code works fine when i provide only one cell like G4 or G4 and H4. The moment I provide the sourcerange as
.Range ("G4", "H4", "G8", "H8", "G10", "H10", "G17", "H17")
the code misbehaves. I get multiple rows for a single entry etc etc. I am not able to understand how to provide this sourcerange so that I get G4, H4, G8, H8, G10, H10, G17, H17 all in one single row inn the sourcerange variable. Any suggestions?
Try this:
Sub myLoop()
Dim r As Range, cel As Range
Set r = Sheet1.Range("G4,H4,G8,H8,G10,H10,G17,H17")
For Each cel In r
Debug.Print cel.Value
Next cel
End Sub
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am quite a beginner in the topic of VBA, that is why I am asking for help.
I need to write a macro that will work more or less like this:
If cell A2 is empty, do not let to fill any other cell.
If there is something in the A2 cell then:
Copy the cell B2 (in which the formula is)
Put the value of the B2 cell to the C2 cell
Delete Column B and let to file other things in the file.
The macro should work automatically. In the sense, let it "apply" in the step of clicking on cell A2. I have a problem with this as a beginner.
Your description is vague, but to get what you described exactly:
Public Sub Process()
If Range("A2").Value <> "" Then
Range("B2").Copy Range("C3")
Range("B2").EntireColumn.Delete
End If
End Sub
Edit
In worksheet code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = Range("A2").Address And Range("A2").Value <> "" Then
Range("C3").Value = Range("B2").Value
Range("B2").EntireColumn.Delete
End If
End Sub
I think it's the closest you can get. It won't fire on every click, but on selection change to A2.
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 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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I was trying to make a VBA script that would calculate the differene between two cells and place it in the third cell. It should work in the following situation:
Three cells are selected. Two of them have values, the third one is blank.
The VBA script is run.
The VBA script calculates the difference between cells with values.
The difference is recorded in the third (blank) cell.
As you could understand such a VBA script should be able to record the difference in a cell on the right from the values, below, on the left or above.
I'm a newbie in vba so my vba coding depends a lot on the forums were similar issues are discussed. But this time I could not find a solution.
Try this:
Dim rng As Range, cel As Range
Set rng = Selection
If rng.Cells.Count <> 3 Then Exit Sub
With Application.WorksheetFunction
If .CountA(rng) <> 2 Then Exit Sub
For Each cel In rng
If cel.Value = "" Then
Select Case True
Case cel.Address = rng(1).Address
cel.Value = rng(2)-rng(3)
Case cel.Address = rng(2).Address
cel.Value = rng(1)-rng(3)
Case cel.Address = rng(3).Address
cel.Value = rng(1)-rng(2)
End Select
Exit For
End If
Next
End With
not tested soi leave it to you.
Edited for simoco