Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all 49926 articles
Browse latest View live

IF Formulas with multiple criterias

$
0
0
Hi everyone,

I have a workbook with different columns containing information.

In column U I have Net Revenues that I want to sum up only and only if in the same row in column N there is a certain number that is NOT in column L at the same time.

So Excel should first check whether the number, e.g. 46999, is only in column N and not L. Then take the Net Revenue of the row in column U and take the sum all the way down.

How can I do that with a formula? Or maybe PivoTable?

Many thanks for your help.

Need a macro to collect the existing size of columns of a particular sheet.

$
0
0
Dear Friends,
I would like to generate a macro to find the existing column sizes of a worksheet (i.e. whatever the columns I have set, the same size should be recorded in vb code).

Like:

Sheets("sheets 1").Activate
Columns("A:A").ColumnWidth = 15.05
Columns("B:B").ColumnWidth = 6.5
Columns("C:C").ColumnWidth = 5.5
Columns("D:D").ColumnWidth = 5.5
Columns("E:E").EntireColumn.AutoFit
Columns("F:F").ColumnWidth = 5.99
Columns("G:G").ColumnWidth = 6.01
Columns("H:H").ColumnWidth = 15.05
Columns("I:I").ColumnWidth = 6.5
Columns("J:J").ColumnWidth = 5.5
And so on…….. till the last column in the sheet which has some value.

I am aware of doing such exercise using “record macro”, but as I have more than 300 columns in each of around 10 sheets, I seek your guidance to generate the codes like above through a macro.

Request your help.

skip folders with no permission in folder mapping

$
0
0
Hello,
i have a code that i found online and modified it a little for my needs. the code is mappig folder and subfolder in a spesific path.

when it comes to a fold with no view permission its stoped.

can anyone help me with modifiing the code so it will map the folder name and move on to the next?

Thank you

Global startFolderPath As String

Public Sub Hierarchical_Folders_and_Files_Listing2()

Dim startFolderPath As String
Dim startCell As Range
Dim n As Long

startFolderPath = Sheet1.Cells(3, 6)

With Sheets("Sheet1")
ActiveSheet.Range("A1:D5000").Value = Delete
.Activate
Set startCell = .Range("A1")
End With

n = List_Folders_and_Files2(startFolderPath, startCell)

End Sub


Private Function List_Folders_and_Files2(folderPath As String, destCell As Range) As Long

Static FSO As Object
Dim thisFolder As Object, subfolder As Object
Dim fileItem As Object
Dim n As Long

If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")

Set thisFolder = FSO.GetFolder(folderPath)



'Add hyperlink for this folder

destCell.Parent.Hyperlinks.Add Anchor:=destCell, Address:=thisFolder.Path, TextToDisplay:=thisFolder.Name

'List subfolders in this folder
If destCell.Column > Sheet1.Cells(4, 6) Then GoTo here

n = 0


For Each subfolder In thisFolder.SubFolders

n = n + 1 + List_Folders_and_Files2(subfolder.Path, destCell.Offset(n + 1, 1))

Next

here:

'Add hyperlink for each file in this folder

List_Folders_and_Files2 = n


End Function

Userform Textbox Text - How to jump to next line

$
0
0
Hello,

I have created a userform with one textbox. In properties I have selected True for Multiline.

Now, when I typing in the textbox, it doesn't allow me to jump to the next line after typing few words.

Let's say I type - Update your initials - How do I jump to next line to enter next data and so on.

Currently, I have to complete the entire line and when it reaches the end, it automatically jumps to the next line.

I'm not looking for a code that will link the textbox to a range.

Is there a way to achieve this without linking to range.

open multiple file text and insert the data to sql server

$
0
0
Hello, I have a case here,

I want to open multiple text (TXT) files using vba macros, then smoothed the data first before being transferred to sql server, here's my code to open multiple file, but i still have problem in looping process..

Quote:

Sheets("Branch_Process").Select
For i = 7 To 9
varCellValue = range("H" & i).Value
fileName = "C:\Users\SIMAS-FUJ1\Documents\DataTextfile\" & varCellValue

Workbooks.Open (fileName)
Next i
the problem is, it's only open the first file and i got a messagebox "file C:\Users\SIMAS-FUJ1\Documents\DataTextfile\." cant find or maybe delete.

thankyou..

FYI : cell value in H7 until H9 is the name of the file txt,, example : ASR_ID0010183_CRF.BLSHEET1.SUMMARY_20151231.txt, etc

[SOLVED] Macro Row to Column with Invoice Number

$
0
0
Hi All,

I'm have Sheet "List" with Database and want use macro in Sheet "RESULT" with Invoice Number.

Example :

LIST
inv name item_1 qty_1 price_1 disc_1 total_1 item_2 qty_2 price_2 disc_2 total_2
1 ALPHA ITEM A 1 100 0 100 ITEM AA 2 10 0 20
2 BETA ITEM B 1 200 0 200 ITEM BB 2 20 0 40
3 CHARLITE ITEM C 1 300 0 300
4 DELTA ITEM D 1 400 0 400 ITEM DD 2 40 0 80

RESULT

INV NAME ITEM QTY PRICE DISC TOTAL
1 ALPHA ITEM A 1 100 0 100
1 ALPHA ITEM AA 2 10 0 20
2 BETA ITEM B 1 200 0 200
2 BETA ITEM BB 2 20 0 40
3 CHARLITE ITEM C 1 300 0 300
4 DELTA ITEM D 1 400 0 400
4 DELTA ITEM DD 2 40 0 80

Thanks

Search and mark all hits from one sheet

$
0
0
Hi,

Kinda odd question but I would like a sheet (With serialnumbers) and run a script that searches through all workbook and marking all the hits with a color.

what I am trying to achieve is verifiying that all the serialnumbers are in use and in case they are not make sure they get used :)

This is a workbook with 25 sheets (different locations) and that is what makes it an issue.

Note. I have all the serialnumbers that are used, and all serialnumbers that are assigned to the different location. This is a search and mark mission :)

Hide rows depending on a criteria entered in a dialogue box.

$
0
0
Hello,

As the text filter or advanced filter are limited, I am looking for a vba code, which hides the rows that contain certain words in column A, after typing them into a dialogue box.
Manually deselecting over 10k+ rows is dragging....

For example: PC, Alien, download, car, green. This would hide all rows with those words in.
Or if it is easier, I would rerun the macro and use 1 criteria at a time.


Regards,

RR

[SOLVED] vba to match two criteria and copy rows to different worksheet

$
0
0
Hi,

I am struggling with my code shown below

The code looks at the data in worksheet Data and copies rows match the criteria shown in cell C2 in the ASM & SM detail worksheet and then anything with YES in column W on the Data worksheet.

The code contains no errors but is only copying one row. I require the macro to copy all rows that meet this criteria and copy them into row 21 and below on the ASM & SM Detail sheet.

The data in the Data worksheet start from row 7 (with row 6 being headers). I have completed this macro before using filters, but it takes a long time and am trying to quicken the process



Code:

Sub ASMRows()
   
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
   
   
    Dim ws1 As Worksheet, ws2 As Worksheet
 
   
    Set ws1 = Sheets("Data")
    Set ws2 = Sheets("ASM & SM Detail")
    Set DataRng = ws1.Range("A6:AU30000")
 
    ws2.Activate
    Range("a8:aU17").Select
    Selection.ClearContents
   
    ws2.Activate
    Range("a21:aU99999").Select
    Selection.ClearContents
   
    Dim i As Long
      Dim n As Long
        Dim iLastRow As Long
    Dim iTarget As Long
   
    With ws1
   
    n = 21
       
        iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
       
        For i = 7 To iLastRow
            If .Cells(i, "G").Value = ws2.Range("C2") And _
            .Cells(i, "W").Value = "YES" Then
                iTarget = iTarget + 1
                .Rows(i).Copy ws2.Range("A" & n)
            End If
        Next i
End With
  Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

    End Sub

Thank you for your help

How to set command for merge data in a column.

$
0
0
Case liked this:
I has done command :click on SUGAR button that when click will fill on next sheet's column liked:Sugar

but is there a command that can filled the column liked: Sugar, Sugar, Sugar ;when i click multiple time on the button ?

Copy Range (B2&B3) from excel to a textbox in Powerpoint

$
0
0
Hi guys,

Hope you are well.

I need help in order to copy a range from excel to a text box in Powerpoint.
Also if I have a static text like "Report of" and i have to put the value from D4 from excel, how can i customize the code so that I have my values into the text box.

Please find attached my code, that is not working properly.
1.png
Code:

For Each ws In Worksheets
   
    Z = Z + 1
    ws.Activate

    Set ppSlide = ppPres.Slides.Add(Z, ppLayoutBlank)
    Set ppTextbox = ppSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 20, 20, 400, 60)
        With ppTextbox.TextFrame
            .TextRange.Text = "VESSEL COST PERFORMANCE MATRIX – "
            .TextRange.ParagraphFormat.Alignment = ppAlignLeft
            .TextRange.Font.Size = 16
            .TextRange.Font.Name = "Arial"
            .VerticalAnchor = msoAnchorMiddle
            .TextRange.Font.Bold = True
       
             
        End With
   
    Set ppTextbox1 = ppSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 20, 40, 250, 80)
   
        With ppTextbox.TextFrame
            .TextRange.Text =  "My Report from" & Range ("B3&B7").Value
            .TextRange.ParagraphFormat.Alignment = ppAlignLeft
            .TextRange.Font.Size = 16
            .TextRange.Font.Name = "Arial"
            .VerticalAnchor = msoAnchorMiddle
            .TextRange.Font.Bold = True
       
             
        End With

    For Each CH In ActiveSheet.ChartObjects
             
              CH.Activate
              ActiveChart.ChartArea.Select
              ActiveChart.ChartArea.Copy

              ppSlide.Shapes.Paste.Select
             
              ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, msoTrue
             
              ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, msoTrue
             
        Next CH
       
 Next ws

Thank you in advance,
Ionut

Create text fil with caracter set (codepage) different from default

$
0
0
Hi all,

I’m trying to create a code that pulls data, text and numbers, from a workbook and puts it in a flat text file. So far it is straight forward.

Now the thing is that the text file should follow an industry standard (called SIE) to be able to import it to other applications. This standard stipulates that the character set should be ASCII codepage 437 (aka. PC-8, OEM437 or MS-DOS Latin US). This is where things get complicated since VBA uses the computers default codepage (I think that it in my case is 1252 Latin I but I’m not really sure). Also, since I live in Sweden, me and my colleagues us a lot of the characters Å, Ä and Ö so it is of importance to get those signs correct in the receiving system.

Does anybody have any suggestions how to get a string from Excel/VBA into a textfile converted to a codepage that differs from the default?

I have spent quite a few hours searching the web but without any luck. There are lots of posts about how to import a text file with a specific codepage but how about the other way around?

BTW, my level of knowledge in VBA is low (leaning towards medium).

regards Hebbe

Macro for getting data??

$
0
0
Hi I am new to this side of Excel and hoping someone may be able to assist?
I have a worksheet from which I run a report weekly. I have learnt how do do some basic macros, so when I run the report I can now clear the data and get the data for the report.
However.....
One of the columns is "Date". On the report I have a beginning date and end date. So I only want to get the rows with a date that is between the ones entered.
Should this be easy?? I have tried writing some code but it seems well beyond me at the moment. Should I be using "if" statements?
Any help would be gratefully received

excel vba error 91 object variable or with block variable not set

$
0
0
Hi everybody,

I have some VBA code wich generates a bug. The message is : "excel vba error 91 object variable or with block variable not set"

The bug code is :
strAdrDateEnvoiRelance = Range("C1:C100").Find(DateSerial(Mid(strAyJourOuvréMois(i), 7, 4), Mid(strAyJourOuvréMois(i), 4, 2), Mid(strAyJourOuvréMois(i), 1, 2)), Range("C1")).Address(False, False)

Formatting tags added by mark007
The value of the variable
strAyJourOuvréMois(i)=01/04/2016


The vba code run when the sheet (Solde mensuel 2016) from the workbook (Palettes europe BMF 2016) is activated. and then it looks for values less than -100 , copy them paste them in another workbook, check for the net work days of the month and then creating a table in the top of the same sheet.

all the code runs well, but the last section when it hae to lookfor values in the table create a bug. But i really don't know how to fix this bug. I checked almost every posts concerning this type of error, i tried the same synthax of the error line in another sheet and it works.

Can somebody see why it doesn't work?
Thanks for your help

Need formula to compare A&B column values after every hyphen sign

$
0
0
Dear experts

I have range of data in A:B with delimited of hypen - i want compare A & B column get result in D through value of each before delimiter of hypen


i need formula to compare get result as like value after hyphen
:Co_Code-Account-LOB-Sub Account-ICO-CURRENCY

Like A= 0023-535002-N79-B1451-0000-PLN And B=0023-535002-N79-P2110-0000-PLN
Result Sub Account becoz difference is between B1451&P2110

find the attachment

Thanks in advance
Attached Files

Copying Command Buttons with VBA from one sheet to another

$
0
0
Can anyone tell me how I copy a Command Button from one sheet to another and the code associated with it.

I currently am using the following code to add a worksheet and copy the information within it. I need to add the command buttons to this (I have 2 to copy by the way)

Code:

Private Sub CommandButton1_Click()
Dim NewName As String
Another:
OneMore = False
NewName = InputBox("What Do you Want to Name the New Sheet?")
If NewName = "" Then Exit Sub
Sheets.Add(after:=Sheets(Sheets.Count)).Name = NewName
Sheet2.Cells.Copy Destination:=ActiveSheet.Range("A1")
ActiveSheet.Visible = False
If MsgBox("Do you want to Add another Sheet?", vbYesNo) = 6 Then GoTo Another
Call GetSheets
ActiveWorkbook.RefreshAll
DoEvents
End Sub

Autohide Columns

$
0
0
I'm trying to have columns Y:AW autohide if the value in row 4 = 0. So if Y4 = 0 coumn Y would be hidden. etc. I can't figure out what i'm missing here.

Code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("Y4:AW4").Value = 0 Then
        Columns("Y:AW").EntireColumn.Hidden = True
    Else
        Columns("Y:AW").EntireColumn.Hidden = False
    End If
End Sub

convert 1528, etc. to excel time format

$
0
0
I have time stamps in cells on a spreadsheet that look like this: 1035, 1450, 1256, 1129, etc, on which I need to get the difference in minutes. What is the easiest way of doing this with VB?

If there's no easier way than this, I thought I might convert each one to excel-type format somehow and then just take the difference. How can that be done? (format I'm referring to is where 1035, for example, is represented by 0.440972222222222, and so on).

Thanks

Move all files in a folder to a new folder it datetime less the latest datetime

$
0
0
I am trying to "move" all files of any name in a folder to a new folder who's date time of making is less then the most resent date time found, i.e I only want the most resent file (chosen by date and time of its making not its file name) to remain in the folder all other files moved to a new Folder

I can find the most resent file by date time with the function : LatestDateTime but the sub Copy_Files_Dates is not moving the files

The sub is not executing the If statement

Code:

If FileInFromFolder.DateLastModified < dtNew Then
I have been tring to get this for a long time but am not getting it!

Thanks

Code:

Sub Copy_Files_Dates()
'This example copy all files between certain dates from FromPath to ToPath.
'You can also use this to copy the files from the last ? days
'If Fdate >= Date - 30 Then
'Note: If the files in ToPath already exist it will overwrite
'existing files in this folder
    Dim fso As Object
    Dim dtNew As Date
    Dim FromPath As String
    Dim ToPath As String, Rpath As String
    Dim Fdate As Date
    Dim FileInFromFolder As Object

    FromPath = ThisWorkbook.path & "\Files to Combine\"
    ToPath = ThisWorkbook.path & "\Files to Combine\OldFiles\"

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If

    If Right(ToPath, 1) <> "\" Then
        ToPath = ToPath & "\"
    End If

    Set fso = CreateObject("scripting.filesystemobject")

    If fso.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If

    If fso.FolderExists(ToPath) = False Then
        MsgBox ToPath & " doesn't exist"
        Exit Sub
    End If
   
    dtNew = LatestDateTimeOrFileName(FromPath, "*.xlsx)

    For Each FileInFromFolder In fso.GetFolder(FromPath).files
        If FileInFromFolder.DateLastModified < dtNew Then

            FileInFromFolder.Move ToPath
           
        End If
    Next FileInFromFolder
End Sub


LatestDateTime function:
Code:

Function LatestDateTime(MyPath As String, fileExtns As String) As Date
Dim LMD As Date
Dim MyFile As String
   
    'Make sure that the path ends in a backslash
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
   
    'Get the first Excel file from the folder
    MyFile = Dir(MyPath & "*.xls", vbNormal)
   
    'If no files were found, exit the sub
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If
   
    'Loop through each Excel file in the folder
    Do While Len(MyFile) > 0
   
        'Assign the date/time of the current file to a variable
        LMD = FileDateTime(MyPath & MyFile)
       
        'If the date/time of the current file is greater than the latest
        'recorded date, assign its filename and date/time to variables
        If LMD > LatestDateTime Then
            LatestFile = MyFile
            LatestDateTime = LMD
        End If
       
        'Get the next Excel file from the folder
        MyFile = Dir
       
    Loop
 
End Function

Import Punch card text file to excel via condition time

$
0
0
Hi

i attach file import from punch card and punch card original text
in excel file Some ID have 2 timings (in and out), and some have 4 timings (in and out - and in and out for lunch break) and some have 6 timing (in and out - and in and out for lunch break and in and out for OT)
i want to capturing time only first in and last out of any ID
and on excel sheet I want to show format like this

ID Date Time
4900651 6/1/2016 07:51 In
4900651 6/1/2016 17:06 Out
4900660 6/1/2016 07:52 In
4900660 6/1/2016 20:00 Out
4900666 6/1/2016 07:47 In
4900666 6/1/2016 17:01 Out
4900667 6/1/2016 07:47 In
4900667 6/1/2016 17:02 Out
4900668 6/1/2016 07:56 In
4900668 6/1/2016 17:02 Out

Any ideas?

Regards
N.Sriwimala
Attached Files
Viewing all 49926 articles
Browse latest View live