0

I need some help, I'm trying to convert my existing VBA macros into C# and using them with a WinForm app. I've gotten most of this macro converted, but I'm running into a wall when I try to convert the below section. For an overview, the macro takes data from the first spreadsheet, filters Column C and then copies and pastes it into a new sheet while naming the new sheet the value of Column C. Column AA just takes all of the unique values of column C and lists them separately.

For Each x In Range([AA2], Cells(Rows.Count, "AA").End(xlUp))
If Not GetWorksheet(x.Text) Is Nothing Then
Sheets(x.Text).Delete
End If

With rng
.AutoFilter
.AutoFilter Field:=3, Criteria1:=x.Value
.SpecialCells(xlCellTypeVisible).Copy

Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
ActiveSheet.Paste
End With
Next x

I'm really not super familiar with C# and am very trial and erroring my way through it. Any help getting this to work in C#, is greatly appreciated!!

I haven't been able to really get anything solid that would work.

  • 1
    A winforms app doesn't have "sheets" that you can filter and copy. It's not at all clear what you have in mind here. It would make more sense to post the C# code, an explanation of what it is meant to do, and pointing out what isn't working. Then we could perhaps help you with the new code you are writing. – topsail Apr 10 '23 at 20:36
  • For [Interop.Excel](https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel?view=excel-pia) you may be able to extract some code from the following posts: https://stackoverflow.com/a/65053425/10024425, https://stackoverflow.com/a/68667926/10024425, https://stackoverflow.com/a/72872634/10024425, and https://stackoverflow.com/a/71755749/10024425. However, you may consider using NuGet package [DocumentFormat.OpenXml](https://github.com/dotnet/Open-XML-SDK#open-xml-sdk) or [ClosedXml](https://github.com/dotnet/Open-XML-SDK#related-tools) instead. – Tu deschizi eu inchid Apr 10 '23 at 20:40
  • Sorry, my problem is, I don't even really know where to start to get this into C#. And the WinForms app is just a button that runs this code in Excel in the background, I just mentioned it to give context on why I was converting to C#. Hopefully someone else that has been in this scenario sees this and can help. – asmcrae1997 Apr 10 '23 at 20:53
  • _I don't even really know where to start to get this into C#_: You may benefit from writing some pseudo-code. See this [post](https://stackoverflow.com/a/65030908/10024425) to learn how to write pseduo-code. Next, use your mouse and click on each of the URLs in my previous comment reading each of the posts to identify which code will be useful. In your pseduo-code, you should have identified that you need to know how to create or open an Excel file and how to save the Excel file - these things can be extracted from the posts. – Tu deschizi eu inchid Apr 10 '23 at 21:01
  • Show the C# code you are attempting to use. How are you converting from Excel VBA to C# - using what tool or library? – NetMage Apr 10 '23 at 21:23

0 Answers0