Difference between two cells in third cell [closed] - vba

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

Related

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

Creating a summary sheet from multiple workbooks [closed]

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

VBA programming in Excel withOUT ActiveCell [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 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

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