我正在尝试在Excel中进行增量编号,但是要针对特定条件。如果条件不匹配,则应保留现有的单元格详细信息。
图片:
从图片中可以看到,我想在B列中创建一个编号列表,该列表基于D列中相应行中显示的信息。因此,在第二行中,我将以“ 1”,然后仅在“ is_null”和“ equal”的计数增加时才继续扩展。同时,我希望它跳过绿色和蓝色单元格并将内容保持原样。
截至目前,我已完成以下公式:
=COUNTIF($D$1:D2,"is_null")+COUNTIF($D$1:D2,"equals")
这会进行正确的编号,但是会覆盖绿色和蓝色单元,而不是分别将其保留为“ A”和“ stop”。
如果有人可以帮助我解决该问题,那么我应该很好。谢谢!
答案 0 :(得分:0)
我不确定如果在D列中找到字符串“ set_path”或“ stop”,但该值始终是相同的(例如,如果D列=“ set_path”和“如果列D =“停止”),则可以使用以下公式获得所需的结果:
#include <Windows.h>
#include <string>
#include <gdiplus.h>
#pragma comment (lib, "Gdiplus.lib")
// Store the "screenshot" when first launching the program
HBITMAP hbm;
// This draws the cursor coordinates close to the cursor
void DrawCursorCoords(Gdiplus::Graphics &graphics, Gdiplus::Bitmap &bitmap, Gdiplus::Color c)
{
POINT cursorPos;
GetCursorPos(&cursorPos);
std::wstring x = std::to_wstring(cursorPos.x);
std::wstring y = std::to_wstring(cursorPos.y);
graphics.DrawString(x.c_str(), x.length(), &Gdiplus::Font(L"Consolas", 16), Gdiplus::PointF(cursorPos.x, cursorPos.y), &Gdiplus::SolidBrush(c));
graphics.DrawString(y.c_str(), y.length(), &Gdiplus::Font(L"Consolas", 16), Gdiplus::PointF(cursorPos.x, cursorPos.y + 16), &Gdiplus::SolidBrush(c));
}
// Paint our stuff
void Paint(HDC &hdc)
{
Gdiplus::Graphics * gfx = new Gdiplus::Graphics(hdc);
Gdiplus::Bitmap * bmap = new Gdiplus::Bitmap(hbm, (HPALETTE)0);
gfx->DrawImage(bmap, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
if (GetAsyncKeyState(VK_LBUTTON))
DrawCursorCoords(*gfx, *bmap, Gdiplus::Color::Red);
else
DrawCursorCoords(*gfx, *bmap, Gdiplus::Color::Green);
delete gfx;
delete bmap;
}
LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
Paint(hdc);
EndPaint(hwnd, &ps);
break;
}
case WM_TIMER:
{
InvalidateRect(hwnd, NULL, NULL);
break;
}
case WM_CLOSE:
{
DestroyWindow(hwnd);
break;
}
case WM_RBUTTONUP:
case WM_KEYDOWN:
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_ERASEBKGND: return TRUE;
default: return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0L;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
char className[] = "_className";
HWND hwnd = NULL;
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_CROSS);
wc.hbrBackground = (HBRUSH)(0);
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
if (RegisterClass(&wc))
{
hwnd = CreateWindowEx(
WS_EX_TRANSPARENT | WS_EX_TOPMOST,
className, NULL,
WS_POPUP | WS_VISIBLE,
0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
NULL, NULL, hInstance, NULL);
}
if (!hwnd) return FALSE;
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
SetTimer(hwnd, 1, 1, NULL);
return TRUE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Take a screenshot and store it to 'hbm'
HWND hwndDesktop = GetDesktopWindow();
HDC hdcDesktop = GetDC(hwndDesktop);
HDC hdcCapture = CreateCompatibleDC(hdcDesktop);
hbm = CreateCompatibleBitmap(hdcDesktop, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
SelectObject(hdcCapture, hbm);
BitBlt(hdcCapture, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
hdcDesktop, 0, 0, SRCCOPY | CAPTUREBLT);
// Start GDI+
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
MSG msg;
InitInstance(hInstance, nCmdShow);
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Gdiplus::GdiplusShutdown(gdiplusToken);
DeleteObject(hbm);
ReleaseDC(hwndDesktop, hdcDesktop);
DeleteDC(hdcDesktop);
DeleteDC(hdcCapture);
return msg.wParam;
}
更新:
使用VBA,您可以按原样保留单元格的内容,而无需使用以下代码用公式覆盖它们:
=IF(AND(D2<>"set_path",D2<>"stop"),COUNTIF($D$1:D2,"is_null")+COUNTIF($D$1:D2,"equals"),IF(D2="set_path","a",IF(D2="stop","stop","")))
第二次更新:
如果在D列中找到“ set_path”,我现在对公式进行了调整,使字母增加了一个(请记住,这将从AZ开始,然后将按照ASCII表开始经过符号,因此如果您想要替代行为,则可能需要对此进行修改):
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set the worksheet you are working with, amend as required.
Dim LastRow As Long, i As Long
LastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
'get the last row with data on Column D
For i = 2 To LastRow
If ws.Cells(i, "B").Value = "" Then 'if cell is empty add formula
ws.Cells(i, "B").FormulaR1C1 = "=COUNTIF(R1C4:RC[2],""is_null"")+COUNTIF(R1C4:RC[2],""equals"")"
End If
Next i
End Sub